uWSGI - ImportError:没有名为os的模块

时间:2015-09-22 18:56:28

标签: python django uwsgi

这很令人费解,因为uWSGI在运行时起作用:

uwsgi --http :8003 --wsgi-file bigsky.wsgi

但不适用于:

uwsgi --ini uwsgi.ini --no-site

它返回此错误:

Traceback (most recent call last):
  File "bigsky.wsgi", line 1, in <module>
    import os
ImportError: No module named os

部署时使用:

  • python 3.4
  • django 1.8
  • Centos 7
  • uWSGI 2.0.11

以下是代码:

uwsgi.ini:

[uwsgi]
# %d is the dir this configuration file is in

socket = 127.0.0.1:8003
chmod-socket = 666

uid = bsdev
gid = bsdev

master = true
enable-threads = true
processes = 4

# virtualenv
home = /home/bsdev/.virtualenvs/bs_py34

chdir = /www/django/releases/persistent/bsrs/python3/
wsgi-file = project.wsgi

env = DJANGO_SETTINGS_MODULE=project.settings.persistent

# create a pidfile
pidfile = /tmp/project-master.pid 
harakiri = 10 # respawn processes taking more than 20 seconds
max-requests = 5000 # respawn processes after serving 5000 requests

# will be run via `supervisord` so don't need to daemonize
# logto = /var/log/uwsgi/project.log
logdate = true
vacuum = true

project.wsgi:

import os
import sys


# python 3
sys.path.append('/usr/local/bin/python3.4')
sys.path.append('/usr/local/lib/python3.4/site-packages')

# project
sys.path.append('/www/django/releases/persistent/bsrs/python3')
sys.path.append('/www/django/releases/persistent/bsrs/bsrs-django')
sys.path.append('/www/django/releases/persistent/bsrs/bsrs-django/project')
sys.path.append('/www/django/releases/persistent/bsrs/bsrs-django/project/project')


# 1st instantiate wsgi
from django.core.wsgi import get_wsgi_application
_application = get_wsgi_application()

# Add the app directories to the PYTHONPATH
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings.persistent'


def application(environ, start_response):
    return _application(environ, start_response)

修改

我也收到此错误消息。它说 Python版本:2.7.5 ,但Django应该在python 3.4中运行。这可能是一个问题吗?

Tue Sep 22 13:25:12 2015 - thunder lock: disabled (you can enable it with --thunder-lock)
Tue Sep 22 13:25:12 2015 - uwsgi socket 0 bound to UNIX address /tmp/bigsky.sock fd 3
Tue Sep 22 13:25:12 2015 - Python version: 2.7.5 (default, Jun 24 2015, 00:41:19)  [GCC 4.8.3 20140911 (Red Hat 4.8.3-9)]
Tue Sep 22 13:25:12 2015 - Set PythonHome to /home/bsdev/.virtualenvs/bs_py34/
Tue Sep 22 13:25:12 2015 - Python main interpreter initialized at 0x25258f0
Tue Sep 22 13:25:12 2015 - python threads support enabled
Tue Sep 22 13:25:12 2015 - your server socket listen backlog is limited to 100 connections
Tue Sep 22 13:25:12 2015 - your mercy for graceful operations on workers is 60 seconds
Tue Sep 22 13:25:12 2015 - mapped 363840 bytes (355 KB) for 4 cores
Tue Sep 22 13:25:12 2015 - *** Operational MODE: preforking ***
Traceback (most recent call last):
  File "./bigsky/wsgi.py", line 1, in <module>
    import os
ImportError: No module named os

2 个答案:

答案 0 :(得分:1)

我遇到了这个问题,因为我没有在Centos 7上正确地编译uWSGI,需要用Python 3.4构建它。简而言之,这是命令:

wget http://projects.unbit.it/downloads/uwsgi-2.0.3.tar.gz
tar -xvf uwsgi-2.0.3.tar.gz
cd uwsgi-2.0.3

source ~/path/to/virtualenv_python3.4/bin/activate

python uwsgiconfig.py --build

然后运行uWSGI:

sudo ~/misc/uwsgi-2.0.3/uwsgi --ini uwsgi.ini

答案 1 :(得分:0)

您正在使用python3,因此将uwsgipip3一起安装

pip3 install uwsgi