Gunicorn,没有名为“ myproject”的模块


68

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

过去,我曾使用Gunicorn + nginx来使该应用程序保持活动状态(基本上在本教程之后),但是在这里遇到了问题。

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.

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

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

因此,没有运行节目的“主要”模块,这正是我期望gunicorn所寻找的。

有什么想法吗?

wsgi.py

import os

from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()

1
哪里myproject.wsgi呢 它的内容是什么?
Plasma

@Plasma我刚刚更新了问题,以包括-的内容,据wsgi.py我了解,这就是gunicorn在寻找的东西,我弄错了吗?
好主意

如果您通过执行来运行gunicorn gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application,那么gunicorn将查找该文件myproject.wsgi并使用该application文件中调用的变量。
Plasma

我只是用裸露的django安装尝试了这一点,区别在于venv该应用程序上方是一个目录。因此,我们有:[...]/myproject/venv[...]/myproject/myproject/wsgi.py---这可行。(没有myproject.wsgi
好主意

Answers:


92

您的错误消息是

ImportError: No module named 'myproject.wsgi'

您与运行应用程序

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

和wsgi.py有线

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

这是断开连接。为了认识该项目作为myproject.wsgi目录必须是python的路径上运行...

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

将消除该错误。但是,您将得到另一个错误,因为wsgi.py文件引用了settings而不是myproject.settings。这意味着该应用程序应从根目录而不是一个目录中运行。您可以通过查看代码确定这一点-如果代码使用绝对导入,它们通常会说from myproject.app import ...还是from app import ...。如果该猜测正确,则您的正确命令是

gunicorn --bind 0.0.0.0:8000 wsgi:application

如果应用程序确实myproject在所有路径中都使用过,则必须修改PYTHONPATH才能正确运行它...

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

2
非常感谢,这条线对我PYTHONPATH=/.. gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
有用

1
我的困扰是命名约定是python的约定,而不是目录结构。例如:myproject.wsgi对应于扩展名myproject/wsgi.py而不是文件myproject.wsgi所在的位置wsgi。感谢您的帮助!
Asher Mancinelli '18

请问myprojectmyproject.wsgi代表该文件夹的名字吗?还是Django项目名称?就我而言,文件夹名称是不同的,我不能同时使用这两个名称
otong

那是一个fully qualified module name。在通常情况下,此路径中的名字将是包含__init__.py文件的顶层目录。
Paul Becotte

(这与您使用的路径相同from myproject.wsgi import application
Paul Becotte

2

替换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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.