问题
I've been working with AppEngine for a little while now and always have a bit of friction when using external libraries where I need to modify each import statement to reflect the fact that the library is not in the base directory of the project.
For example, my directory structure might look like (with the requests library installed)
/myapp/app.yaml
/myapp/main.py
/myapp/libraries/requests/packages/
/myapp/libraries/requests/__init__.py
/myapp/libraries/requests/adapters.py
etc...
However appengine expects something like
/myapp/app.yaml
/myapp/main.py
/myapp/requests/packages/
/myapp/requests/__init__.py
/myapp/requests/adapters.py
/myapp/requests/api.py
etc...
I then need to go in to each of the requests files to modify the imports to something like
from libraries.requests import adapters
rather than simply
import adapters
which may be within api.py. If I do not modify it I get an import error e.g.
ImportError: No module named adapters
I have looked at the following How to import modules in Google App Engine? which suggests modifying the path (though the application is different in their code) using
sys.path.append(os.path.join(os.path.dirname(__file__), 'libraries'))
To translate this to my issue I could put something like
sys.path.append(os.path.dirname(__file__))
at the start of each __init__.py file within libraries. However it strikes me that there must be a more efficient way, and in keeping with DRY - but I cannot seem to find it. I've tried simply including it in the top most __init__.py file within libraries and then again in the top most __init__.py file within each package. This does not work for sub-packages though unfortunately. Is there a way to make one change to fix this?
回答1:
I have all my third party libs in a lib
dir
The I just include
sys.path.insert(0,'./lib')
in appengine_config.py which is loaded before any of your code.
If find it needs to be inserted at the beginning of sys.path
rather than at then end if you use specific versions of libs like webob that are not part of the appengine available versions.
Then you don't have to modify any of the libraries etc....
This is pretty standard python behaviour. In a normal desktop install situation you are installing vie easy_install which sticks stuff in places like site-packages and setups paths etc.... but if you aren't using any of that, and you can't with appengine you have to do the path manipulation yourself.
来源:https://stackoverflow.com/questions/18248784/how-can-i-simply-modify-the-path-for-the-entire-requests-package-within-a-librar