Python Gunicorn,没有名为“myproject”的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39460892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gunicorn, no module named 'myproject
提问by Good Idea
I'm installing a previously built website on a new server. I'm not the original developer.
我正在新服务器上安装以前构建的网站。我不是原始开发人员。
I've used Gunicorn + nginx in the past to keep the app alive (basically following this tutorial), but am having problems with it here.
我过去曾使用 Gunicorn + nginx 来保持应用程序的活动状态(基本上遵循本教程),但在这里遇到了问题。
I source venv/bin/activate
, then ./manage.py runserver 0.0.0.0:8000
works well and everything is running as expected. I shut it down and run gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
, and get the following:
I source venv/bin/activate
,然后./manage.py runserver 0.0.0.0:8000
运行良好,一切都按预期运行。我关闭它并运行gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
,并得到以下信息:
[2016-09-13 01:11:47 +0000] [15259] [INFO] Starting gunicorn 19.6.0
[2016-09-13 01:11:47 +0000] [15259] [INFO] Listening at: http://0.0.0.0:8000 (15259)
[2016-09-13 01:11:47 +0000] [15259] [INFO] Using worker: sync
[2016-09-13 01:11:47 +0000] [15262] [INFO] Booting worker with pid: 15262
[2016-09-13 01:11:47 +0000] [15262] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/var/www/myproject/venv/lib/python3.5/site-packages/gunicorn/arbiter.py", line 557, in spawn_worker
worker.init_process()
File "/var/www/myproject/venv/lib/python3.5/site-packages/gunicorn/workers/base.py", line 126, in init_process
self.load_wsgi()
File "/var/www/myproject/venv/lib/python3.5/site-packages/gunicorn/workers/base.py", line 136, in load_wsgi
self.wsgi = self.app.wsgi()
File "/var/www/myproject/venv/lib/python3.5/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/var/www/myproject/venv/lib/python3.5/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
return self.load_wsgiapp()
File "/var/www/myproject/venv/lib/python3.5/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
return util.import_app(self.app_uri)
File "/var/www/myproject/venv/lib/python3.5/site-packages/gunicorn/util.py", line 357, in import_app
__import__(module)
ImportError: No module named 'myproject.wsgi'
[2016-09-13 01:11:47 +0000] [15262] [INFO] Worker exiting (pid: 15262)
[2016-09-13 01:11:47 +0000] [15259] [INFO] Shutting down: Master
[2016-09-13 01:11:47 +0000] [15259] [INFO] Reason: Worker failed to boot.
I believe it something to do with the structure of the whole application. Before, I've built apps with the basic structure of:
我相信这与整个应用程序的结构有关。之前,我构建了具有以下基本结构的应用程序:
myproject
├── manage.py
├── myproject
│?? ├── urls.py
│?? ├── views.py
│?? ├── component1
│ │?? ├── urls.py
│ │?? └── views.py
│?? ├── component2
│ │?? ├── urls.py
│ │?? └── views.py
├── venv
│?? ├── bin
│?? └── ...
This one, instead, has a structure like:
相反,这个具有如下结构:
myproject
├── apps
│?? ├── blog
│ │?? ├── urls.py
│ │?? ├── views.py
│?? │ └── ...
│?? ├── catalogue
│ │?? ├── urls.py
│ │?? ├── views.py
│?? │ └── ...
│?? ├── checkout
│ │?? ├── urls.py
│ │?? ├── views.py
│?? │ └── ...
│?? ├── core
│ │?? ├── urls.py
│ │?? ├── views.py
│?? │ └── ...
│?? ├── customer
│?? ├── dashboard
│?? └── __init__.py
├── __init__.py
├── manage.py
├── project_static
│?? ├── assets
│?? ├── bower_components
│?? └── js
├── public
│?? ├── emails
│?? ├── media
│?? └── static
├── settings
│?? ├── base.py
│?? ├── dev.py
│?? ├── __init__.py
│?? ├── local.py
│?? └── production.py
├── templates
│?? ├── base.html
│?? ├── basket
│?? ├── blog
│?? └── ....
├── urls.py
├── venv
│?? ├── bin
│?? ├── include
│?? ├── lib
│?? ├── pip-selfcheck.json
│?? └── share
└── wsgi.py
So, there's no 'main' module running the show, which is what I expect gunicorn is looking for.
因此,没有运行该节目的“主要”模块,这正是我期望 gunicorn 所寻找的。
any thoughts?
有什么想法吗?
wsgi.py:
wsgi.py:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
application = get_wsgi_application()
回答by Paul Becotte
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
wsgi.py 有这条线
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
This is the disconnect. In order to recognize the project as myproject.wsgi
the parentdirectory would have to be on the python path... running
这就是脱节。为了认识该项目作为myproject.wsgi
的父目录必须是python的路径上运行...
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
会消除那个错误。但是,您会得到一个不同的错误,因为 wsgi.py 文件引用settings
而不是myproject.settings
. 这意味着该应用程序旨在从根目录而不是一个目录运行。您可以通过查看代码来确定这一点 - 如果它使用绝对导入,他们通常会说from myproject.app import ...
或from app import ...
。如果那个猜测是正确的,你的正确命令是
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...
如果应用程序确实myproject
在所有路径中使用,则必须修改 PYTHONPATH 以正确运行它...
PYTHONPATH=`pwd`/.. gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
回答by Sateesh
Run these command after replacing your python working directory path.
替换 python 工作目录路径后运行这些命令。
# 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