Reputation: 336
I am implementing uwsgi with nginx and I use this structure for my files in my app directory:
myappfolder / my_app
myappfolder / my_app / main.py
myappfolder / my_app_venv
myappfolder / wsgi.py
I have set up a python virtualenv for holding my app and installed uwsgi inside of it (following by the tutorial at the https://www.digitalocean.com/community/tutorials/how-to-deploy-python-wsgi-applications-using-uwsgi-web-server-with-nginx)
In the beginning with the default setting as mentioned in that link the server worked fine and I ran uwsgi with this command: uwsgi --socket 127.0.0.1:12345 -w wsgi &
, but after some tweaking the code in wsgi.py
file and making it this way:
#!/usr/bin/env python3
import sys
sys.path.append('/root/myappfolder')
from my_app.main import Main
fff = Main()
def application(env, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return [fff.sayHello(), str(env)]
Then when I run uwsgi it gives me error from the python interpreter => ImportError: No module named my_app.main
In my_app / main.py
file:
#!/usr/bin/env python3
class Main:
def sayHello(self):
return("hello world!")
What is the cause of the problem?
UPDATE 1: Even after import sys
in the wsgi.py
file and using sys.path.append('the path to myappfolder')
it does not have any effect at all.
Question Simplified: Let me simplify my question: why virtualenv's python interpreter does not find the modules that are alongside the callable file that is used for uwsgi (in my case wsgi.py
file)?
UPDATE 2: I moved main.py
file from my_app
folder to one upper path meaning it is now located at myappfolder / main.py
and now the wsgi.py
file can see and import it. Why when it was under a subdirectory, it was not being seen by wsgi.py
file?
Upvotes: 1
Views: 881
Reputation: 336
I finally found what was going on. uwsgi is using Python 2.7 which requires you to use __init__.py file in each folder module. Even an empty file. The problem is fixed now moving from that directory structure mentioned in the question to the one written below:
myappfolder / my_app
myappfolder / my_app / main.py
myappfolder / my_app / __init__.py # empty file
myappfolder / my_app_venv
myappfolder / wsgi.py
This is the big difference of python 2.X and 3.X which caused me get stuck for nearly two days.
Upvotes: 3