Gunicorn, no module named 'myproject

时间秒杀一切 提交于 2019-11-28 07:13:24

Your error message is

ImportError: No module named 'myproject.wsgi'

You ran the app with

gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application

And wsgi.py has the line

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

This is the disconnect. In order to recognize the project as myproject.wsgi the parent directory would have to be on the python path... running

cd .. && gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application

Would eliminate that error. However, you would then get a different error because the wsgi.py file refers to settings instead of myproject.settings. This implies that the app was intended to be run from the root directory instead of one directory up. You can figure this out for sure by looking at the code- if it uses absolute imports, do they usually say from myproject.app import ... or from app import .... If that guess is correct, your correct commmand is

gunicorn --bind 0.0.0.0:8000 wsgi:application

If the app does use myproject in all of the paths, you'll have to modify your PYTHONPATH to run it properly...

PYTHONPATH=`pwd`/.. gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application

Run these command after replacing your python working directory path.

# Go to your current working directory
cd /path/to/folder

# Activate your virtual environment. Ignore if already in activated mode
source /path/to/virtualenv/bin/activate

# Install gunicorn in virtualenv
pip3 install gunicorn

# Run this command. Replace PORT and app name accordingly
gunicorn --bind 0.0.0.0:5000 wsgi:app
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!