I am developing a Flask app that runs using uwsgi. I'm running into the following error ImportError: No module named google.oauth2.credentials
.
When I run the Flask app using Flask's builtin dev server, the application runs without issues.
The problem seems to stem from how the google-auth
module is installed. There's no __init__.py
file in the google
namespace folder in site-packages
. The full path being /usr/local/lib/python2.7/site-packages/google
.
I'm aware that python3.3 has implicit namespace packaging via PEP-420.
What I'm confused about is how the implicit namespace packaging works in python2.7. Does the installation process via pip
do something special?
I'm also confused about what is different when running python2.7 via uwsgi.
Relevant versions
- python 2.7.14
- pip 9.0.3
- uwsg 2.0.13
- google-auth 1.4.1
- Installed via pip (not using virtualenv)
- https://github.com/GoogleCloudPlatform/google-auth-library-python
I've also included the following barebones test to reproduce the error.
test.ini
[uwsgi]
socket = 0.0.0.0:5001
plugins-dir = /usr/lib/uwsgi/
plugins = python
protocol = uwsgi
pythonpath = /usr/local/lib/python2.7/site-packages
callable = app
max-requests = 1000
master = True
lazy-apps = True
processes = 1
test.py
import google.oauth2.credentials
working command
python test.py
failing command
uwsgi --ini test.ini --wsgi-file test.py
failing result
When I run the above command, the program will fail due to the following ImportError.
Traceback (most recent call last):
File "test.py", line 1, in <module>
import google.oauth2.credentials
ImportError: No module named google.oauth2.credentials
Current workaround
My current workaround is to manually add an __init__.py
file with the following (which is what is basically what is found in the google-auth repository, but for some reason is removed when installing via pip):
try:
import pkg_resources
pkg_resources.declare_namespace(__name__)
except ImportError:
import pkgutil
__path__ = pkgutil.extend_path(__path__, __name__)
Workaround 2
Another way I got the import working was by doing this:
import site
site.addsitedir('/usr/local/lib/python2.7/site-packages')
import google.oauth2.credentials
This seems to imply that uwsgi is not fully initialization python at least on initial start up.
I got the hint to try site.addistedir
from this answer: https://stackoverflow.com/a/15209116/177646