Django - Foreman无法找到已安装的模型

时间:2016-04-26 19:09:16

标签: python django foreman procfile

我正在尝试使用Foreman / Honcho来管理基于Procfile的Django应用程序。当我启动应用程序查看正常python manage.py runserver时,一切正常。但是,当我通过honcho startforeman start web启动应用时,我收到此错误:

11:59:31 system | web.1 started (pid=27959)
11:59:31 web.1  | [2016-04-26 11:59:31 -0700] [27959] [INFO] Starting gunicorn 19.4.5
11:59:31 web.1  | [2016-04-26 11:59:31 -0700] [27959] [INFO] Listening at: http://0.0.0.0:5000 (27959)
11:59:31 web.1  | [2016-04-26 11:59:31 -0700] [27959] [INFO] Using worker: sync
11:59:31 web.1  | [2016-04-26 11:59:31 -0700] [27962] [INFO] Booting worker with pid: 27962
11:59:31 web.1  | [2016-04-26 18:59:31 +0000] [27962] [ERROR] Exception in worker process:
11:59:31 web.1  | Traceback (most recent call last):
11:59:31 web.1  |   File "/Library/Python/2.7/site-packages/gunicorn/arbiter.py", line 515, in spawn_worker
11:59:31 web.1  |     worker.init_process()
11:59:31 web.1  |   File "/Library/Python/2.7/site-packages/gunicorn/workers/base.py", line 122, in init_process
11:59:31 web.1  |     self.load_wsgi()
11:59:31 web.1  |   File "/Library/Python/2.7/site-packages/gunicorn/workers/base.py", line 130, in load_wsgi
11:59:31 web.1  |     self.wsgi = self.app.wsgi()
11:59:31 web.1  |   File "/Library/Python/2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
11:59:31 web.1  |     self.callable = self.load()
11:59:31 web.1  |   File "/Library/Python/2.7/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
11:59:31 web.1  |     return self.load_wsgiapp()
11:59:31 web.1  |   File "/Library/Python/2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
11:59:31 web.1  |     return util.import_app(self.app_uri)
11:59:31 web.1  |   File "/Library/Python/2.7/site-packages/gunicorn/util.py", line 357, in import_app
11:59:31 web.1  |     __import__(module)
11:59:31 web.1  |   File "../wsgi.py", line 17, in <module>
11:59:31 web.1  |     application = get_wsgi_application()
11:59:31 web.1  |   File "/Library/Python/2.7/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
11:59:31 web.1  |     django.setup()
11:59:31 web.1  |   File "/Library/Python/2.7/site-packages/django/__init__.py", line 18, in setup
11:59:31 web.1  |     apps.populate(settings.INSTALLED_APPS)
11:59:31 web.1  |   File "/Library/Python/2.7/site-packages/django/apps/registry.py", line 85, in populate
11:59:31 web.1  |     app_config = AppConfig.create(entry)
11:59:31 web.1  |   File "/Library/Python/2.7/site-packages/django/apps/config.py", line 90, in create
11:59:31 web.1  |     module = import_module(entry)
11:59:31 web.1  |   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
11:59:31 web.1  |     __import__(name)
11:59:31 web.1  | ImportError: No module named django_messages
11:59:31 web.1  | [2016-04-26 18:59:31 +0000] [27962] [INFO] Worker exiting (pid: 27962)
11:59:31 web.1  | [2016-04-26 11:59:31 -0700] [27959] [INFO] Shutting down: Master
11:59:31 web.1  | [2016-04-26 11:59:31 -0700] [27959] [INFO] Reason: Worker failed to boot.
11:59:31 system | web.1 stopped (rc=3)

这是尝试安装django-message模块。我和其他模块也有同样的问题。我也遇到了与django-webpack-loader相同的问题。我还应该提一下,我在virtualenv和停用时都收到错误。

这是安装django-messages的命令:

$> pip install django-messages
Requirement already satisfied (use --upgrade to upgrade): django-messages in ./lib/python2.7/site-packages

已安装的应用

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'my_app',
    'django_messages',
)

我不确定我可以提供哪些其他信息来帮助排除故障,但基本问题是我如何安装应用程序以与foreman / honcho合作?

1 个答案:

答案 0 :(得分:3)

Honcho和Foreman不使用你的virtualenv中的Python可执行文件和库,虽然你没有包含你的Honcho Procfile,但只需调用python就可以使用系统范围的可执行文件和库。

不幸的是,你不能只调用/path/to/virtualenv/bin/activate作为Procfile的一部分,因为当其中一个子进程退出时,Honcho会退出,如in this Github issue thread所述。但是,您可以使用&&运算符在一个子shell中执行此脚本和python脚本,将它们链接在一起:

web: source venv/bin/activate && python manage.py 

或者,您可以更好地修改您的wsgi.py包装器,以便在导入Django应用程序之前显式提取您的virtualenv库:

# Activate your virtual env
activate_env=os.path.expanduser("/path/to/virtualenv/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))

这些应该在导入任何模块(os之外)之前执行,以确保您的应用程序读取正确的站点库。

最后,Honcho本身支持在Procfile中使用.env文件,这些文件设置了运行进程的环境。此文件的格式与任何bash脚本相同。您可以使用.env文件将PYTHONPATHPYTHONHOME设置为指向Virtualenv中的库,然后在Procfile中指定Virtualenv中的显式Python解释器。

.env文件

PYTHONHOME=/path/to/virtualenv/lib/python2.7
PYTHONHOME=