ImportError:启动Gunicorn的Shell脚本无法找到模块

时间:2015-01-27 12:47:59

标签: django bash shell gunicorn

根据Django REST frameworkvirtualenv给出“productsapi”应用程序的以下文件夹结构。

/webapps/
└── projects
    ├── bin
    │   ├── activate
    │   ├── activate.csh
    │   ├── activate.fish
    │   ├── activate_this.py
    │   ├── django-admin
    │   ├── django-admin.py
    │   ├── easy_install
    │   ├── easy_install-2.7
    │   ├── gunicorn
    │   ├── gunicorn_django
    │   ├── gunicorn_paster
    │   ├── gunicorn_start.sh
    │   ├── pip
    │   ├── pip2
    │   ├── pip2.7
    │   ├── python
    ├── include
    ├── lib
    ├── local
    ├── logs
    ├── run
    ├── static
    └── productsapi
        ├── products
        ├── manage.py
        ├── requirements.txt
        └── productsapi

为此,我想准备一个start script for gunicorn as described an article from 2013。我想出了以下内容:

#!/bin/bash

NAME="productsapi"                           # Name of the application
DJANGODIR=/webapps/projects/                 # Django project directory
USER=exampleuser                             # User to run as
GROUP=examplegroup                           # Group to run as
NUM_WORKERS=3                                # Worker processes to spawn
DJANGO_SETTINGS_MODULE=productsapi.settings  # Settings file should Django use
DJANGO_WSGI_MODULE=productsapi.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

# 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 \
--bind=127.0.0.0:8080 \
--log-level=DEBUG \
--log-file=-

我按如下方式启动脚本:

$ /webapps/projects/bin/gunicorn_start.sh

此操作失败,并显示以下错误消息:

  

ImportError:没有名为productsapi.wsgi的模块

我看起来有些路径设置不正确。

但是,当我执行以下操作时,我可以启动应用程序服务器(请注意不同的路径):

$ cd /webapps/projects/productsapi
$ gunicorn productsapi.wsgi:application --user=exampleuser \
    --groups=exampleuser --bind 127.0.0.1:8080 --log-file=- --log-level DEBUG

1 个答案:

答案 0 :(得分:1)

正如您所注意到的,您的DJANGODIR应为/webapps/projects/productsapi

基本上,它应该是具有manage.py

的目录

您需要在执行行中将./bin/更改为../bin