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?