独角兽号,没有叫做“我的项目”的模块

我正在一个新的服务器上安装一个以前建立的网站。我不是最初的开发人员。

我过去使用过 Gunicorn + nginx 来保持应用程序的活性(基本上遵循 本教程) ,但在这里遇到了问题。

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.

我相信这与整个应用程序的结构有关。在此之前,我构建的应用程序的基本结构如下:

myproject
├── manage.py
├── myproject
│   ├── urls.py
│   ├── views.py
│   ├── component1
│   │   ├── urls.py
│   │   └── views.py
│   ├── component2
│   │   ├── urls.py
│   │   └── views.py
├── venv
│   ├── bin
│   └── ...

相反,它的结构是这样的:

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

所以,没有“主”模块运行的节目,这是我希望枪角兽正在寻找什么。

有什么想法吗?

Wsgi.py :

import os


from django.core.wsgi import get_wsgi_application


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


application = get_wsgi_application()
98836 次浏览

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

If you are using supervisor then you have to set environment in supervisor config file as bellow.

environment=HOME="/home/to/your/project/root"

For my side, My project structure is

myproject
├── manage.py
├── myproject
│   ├── wsgi.py
│   ├── ..
Dockerfile
docker-composer.yml

So in docker-composer.yml, when command

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

i get following error

ModuleNotFoundError: No module named 'myproject.wsgi'

What we have to do is, we must run gunicorn command inside folder, not project root. This is the working code

sh -c "cd ./myproject && gunicorn myproject.wsgi:application --bind 0.0.0.0:8000"

Before gunicorn command, we have to change directory with "cd ./project". Inside the "myproject" directory, gunicorn can recognise our projects clearly.

I faced a similar problem. The gunicorn was being run by a globally installed package, not the one that was installed in the virtual environment. This answer helped me to figure it out.

I was facing similar issue. I recreated the virtual environment and install gunicorn using pip3 (not using apt) and it worked fine