2017-04-18 115 views
0

我想設置我的Django應用程序使用Python 3.5的工作開始uwsgi(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) 

我可以看到,它沒有使用正確的蟒蛇版本。我試圖給本地位置設置爲/home/dhanushka/.virtualenvs/amsAPI但後來我得到

ImportError: No module named site 

回答

3
+0

到我的項目的路徑是/ home/dhanushka/CxenseAMS,我的虛擬環境駐留在/home/dhanushka/.virtualenvs/amsAPI我試着將venv設置爲兩者,但是得到'ImportError:No module named site'。我怎樣才能找出到虛擬env的正確路徑應該是什麼? '(amsAPI)dhanushka @ xxx:/ etc/uwsgi $ echo $ VIRTUAL_ENV /home/dhanushka/.virtualenvs/amsAPI' –

0

在PYTHONPATH參數的路徑必須到python3解釋,裏面的virtualenv:

/[絕對路徑針對Env] /斌/ python3

+0

沒有工作。我嘗試了'venv =/home/dhanushka/.virtualenvs/amsAPI/lib/python3.5/site-packages /',並且沒有站點包 –

+0

也許是參數'pythonpath'。編輯我的答案以反映它。 – XaviP