Gunicorn无法找到我的wsgi文件,无法加载Python吗?
我正在尝试运行一个gunicorn脚本,跟着我在网上找到的教程。
这是我的文件夹结构:
(today_project)[littlem@server1 today_project]$ tree . -L 2
.
├── bin
│ ├── activate
│ ├── activate.csh
│ ├── activate.fish
│ ├── activate_this.py
│ ├── django-admin.py
│ ├── django-admin.pyc
│ ├── easy_install
│ ├── easy_install-2.7
│ ├── gunicorn
│ ├── gunicorn_django
│ ├── gunicorn_paster
│ ├── gunicorn_start
│ ├── pip
│ ├── pip2
│ ├── pip2.7
│ ├── python -> python2.7
│ ├── python2 -> python2.7
│ └── python2.7
├── include
│ ├── python2.6 -> /usr/include/python2.6
│ └── python2.7 -> /usr/local/include/python2.7
├── lib
│ ├── python2.6
│ └── python2.7
├── manage.py
├── run
│ └── gunicorn.sock
├── today
│ ├── #app files
├── today_project
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── __pycache__
│ ├── settings.py
│ ├── settings.pyc
│ ├── urls.py
│ ├── urls.pyc
│ ├── wsgi.py
│ └── wsgi.pyc
└── TODO.md
当我从我的虚拟环境中运行
gunicorn today_project.wsgi:application
时,一切正常。但是当我运行这个脚本(我几乎是从某个地方复制过来的):
#!/bin/bash
NAME="today" # Name of the application
DJANGODIR=~/today_project/today_project # Django project directory
SOCKFILE=~/today_project/run/gunicorn.sock # we will communicte using this unix socket
USER=ferski # the user to run as
GROUP=ferski # the group to run as
NUM_WORKERS=3 # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=today.settings # which settings file should Django use
DJANGO_WSGI_MODULE=today.wsgi # WSGI module name
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $DJANGODIR
source ../bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec ../bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--log-level=debug \
--bind=unix:$SOCKFILE
我遇到了以下错误:
Traceback (most recent call last):
File "/home/ferski/today_project/lib/python2.7/site-packages/gunicorn/arbiter.py", line 495, in spawn_worker
worker.init_process()
File "/home/ferski/today_project/lib/python2.7/site-packages/gunicorn/workers/base.py", line 106, in init_process
self.wsgi = self.app.wsgi()
File "/home/ferski/today_project/lib/python2.7/site-packages/gunicorn/app/base.py", line 114, in wsgi
self.callable = self.load()
File "/home/ferski/today_project/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 62, in load
return self.load_wsgiapp()
File "/home/ferski/today_project/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load_wsgiapp
return util.import_app(self.app_uri)
File "/home/ferski/today_project/lib/python2.7/site-packages/gunicorn/util.py", line 354, in import_app
__import__(module)
ImportError: No module named today_project.wsgi
2014-04-06 16:09:28 [19420] [INFO] Worker exiting (pid: 19420)
2014-04-06 16:09:28 [19407] [INFO] Shutting down: Master
2014-04-06 16:09:28 [19407] [INFO] Reason: Worker failed to boot.
所以我猜这可能是python版本的问题?老实说,我不知道在执行 export PYTHONPATH=$DJANGODIR:$PYTHONPATH
时我在做什么,但我觉得问题可能出在这里。
1 个回答
1
我觉得你的 DJANGODIR 应该直接是 ~/today_project/
。这个路径是整个项目的根目录,而不是里面的某个子目录。
还有其他一些问题:特别是,因为你是在一个虚拟环境中运行,所以其实你根本不需要去修改 PYTHONPATH。