How to install python package in a specific direct

2019-03-01 16:36发布

问题:

I'm developing a twitter app on google appengine - for that I want to use the Twython library. I tried installing it using pip - but it either installs it in the main python dir, or doesn't import all dependencies.

I can simply copy all the files of Twython to the appengine root dir, and also import manually all the dependency libraries, but that seems awfully wrong. How do I install a package in a specific folder including all it's dependencies?

Thanks

回答1:

If you put the module files in a directory, for example external_modules/, and then use sys.path.insert(0, 'external_modules') you can include the module as it would be an internal module.

You would have to call sys.path.insert before the first import of the module. Example: If you placed a "module.pyd" in external_modules/ and want to include it with import module, then place the sys.path.insert before.

The sys.path.insert() is an app-wide call, so you have to call it only once. It would be the best to place it in the main file, before any other imports (except import sys of course).



回答2:

Use virtual environment and virtual environment wrapper , you dont have to use the wrapper but use it for simplicity..

https://pypi.python.org/pypi/virtualenv



回答3:

If you're installing a package using pip, try this:

Install a Python package into a different directory using pip?

I personally needed libraries that I was just using out of git repos. I just symlinked those libraries instead of installing them. But in that case, I had to manually symlink all the dependencies too.



回答4:

You could use gaenv (package manager I built for app engine). It only creates symlinks and follows the pip requirements.txt format. You can install & use like:

pip install gaenv
# create requirements.txt -> twython
pip install -r requirements.txt
gaenv

This creates symlinks to gaenv_lib/ of all required libraries. Then will ask to add the import statement.