使用虚拟环境启动uwsgi时导入错误

时间:2017-04-18 05:44:39

标签: python django uwsgi importerror

我正在尝试设置我的Django应用程序以使用python 3.5(默认为Python 2.7)

所以我在3.5中创建了一个虚拟环境并部署了我的Django项目。

我现在想要使用uwsgi

/etc/uwsgi/emperor.ini

[uwsgi]
emperor = etc/uwsgi/sites
logto = /var/log/uwsgi

/etc/uwsgi/sites/ams.ini

[uwsgi]
project = AMS
base = /home/user

chdir = %(base)/%(project)
#home = %(base)/.virtualenvs/%(project)
module = %(project).wsgi:application

master = true
processes = 5
socket = %(base)/%(project)/ams.sock
chmod-socket = 666
uid = user
gid = www-data
vacuum = true
logto = /var/log/uwsgi/log.log

/etc/systemd/system/uwsgi.service

[Unit]
Description=uWSGI Emperor service
After=syslog.target

[Service]
ExecStart=/usr/local/bin/uwsgi --http :8000 --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT

Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target

如果我从虚拟环境中启动uwsgi,我可以成功启动它

uwsgi --http :8080 --chdir /home/user/AMS -w AMS.wsgi 
[pid: 31757|app: 0|req: 2/2] 203.94.69.162 () {42 vars in 695 bytes} [Mon Apr 17 10:34:02 2017] GET /ams_tools/ => generated 146 bytes in 76 msecs (HTTP/1.1 200) 2 headers in 80 bytes (1 switches on core 0)

然而,当我启动uswgi作为服务时,我收到以下错误

*** Starting uWSGI 2.0.15 (64bit) on [Tue Apr 18 07:33:41 2017] ***
compiled with version: 5.4.0 20160609 on 17 April 2017 07:06:40
os: Linux-4.8.0-45-generic #48~16.04.1-Ubuntu SMP Fri Mar 24 12:46:56 UTC 2017
nodename: ams-de1
machine: x86_64
clock source: unix
detected number of CPU cores: 8
current working directory: /etc/uwsgi/sites
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
setgid() to 33
set additional group 1002 (admin)
setuid() to 1002
chdir() to /home/dhanushka/CxenseAMS
your processes number limit is 256882
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /home/dhanushka/CxenseAMS/ams.sock fd 3
Python version: 2.7.12 (default, Nov 19 2016, 06:48:10)  [GCC 5.4.0 20160609]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0xf3fd70
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 436560 bytes (426 KB) for 5 cores
*** Operational MODE: preforking ***
Traceback (most recent call last):
  File "./CxenseAMS/wsgi.py", line 19, in <module>
    application = get_wsgi_application()
  File "/home/dhanushka/.virtualenvs/amsAPI/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
    django.setup(set_prefix=False)
  File "/home/dhanushka/.virtualenvs/amsAPI/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/dhanushka/.virtualenvs/amsAPI/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/home/dhanushka/.virtualenvs/amsAPI/lib/python3.5/site-packages/django/apps/config.py", line 199, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "./CxenseAMS/models.py", line 5, in <module>
    class SegmentTrafficAbstract(models.Model):
  File "/home/dhanushka/.virtualenvs/amsAPI/lib/python3.5/site-packages/django/db/models/base.py", line 119, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/home/dhanushka/.virtualenvs/amsAPI/lib/python3.5/site-packages/django/db/models/base.py", line 316, in add_to_class
    value.contribute_to_class(cls, name)
  File "/home/dhanushka/.virtualenvs/amsAPI/lib/python3.5/site-packages/django/db/models/options.py", line 214, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/home/dhanushka/.virtualenvs/amsAPI/lib/python3.5/site-packages/django/db/__init__.py", line 33, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/home/dhanushka/.virtualenvs/amsAPI/lib/python3.5/site-packages/django/db/utils.py", line 211, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/home/dhanushka/.virtualenvs/amsAPI/lib/python3.5/site-packages/django/db/utils.py", line 115, in load_backend
    return import_module('%s.base' % backend_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/dhanushka/.virtualenvs/amsAPI/lib/python3.5/site-packages/django/db/backends/postgresql/base.py", line 24, in <module>
    raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named _psycopg
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 3534)
spawned uWSGI worker 1 (pid: 3537, cores: 1)
spawned uWSGI worker 2 (pid: 3538, cores: 1)
spawned uWSGI worker 3 (pid: 3539, cores: 1)
spawned uWSGI worker 4 (pid: 3540, cores: 1)
spawned uWSGI worker 5 (pid: 3541, cores: 1)

我可以看到它没有使用正确的python版本。我试图将家庭位置设置为/home/dhanushka/.virtualenvs/amsAPI,但后来我得到了

ImportError: No module named site

2 个答案:

答案 0 :(得分:3)

答案 1 :(得分:0)

pythonpath参数中的路径必须是virtualenv中的python3解释器:

/ [env的绝对路径] / bin / python3

相关问题