使用 uWSGI 时导入 psycopg2._psycopg 时出错
Error importing psycopg2._psycopg when using uWSGI
当我尝试 运行 uwsgi --ini <my uwsi ini file>
:
时出现以下导入错误
Traceback (most recent call last):
File "/var/www/mysite/venv/lib/python3.5/site-packages/django/db/backends/postgresql/base.py", line 20, in <module>
import psycopg2 as Database
File "/var/www/mysite/venv/lib/python3.5/site-packages/psycopg2/__init__.py", line 50, in <module>
from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: No module named 'psycopg2._psycopg'
但是,当我 运行 站点
时,一切 运行 都很好
python manage.py runserver --settings=config.settings.prod
psycopg2
已安装,Django 查不到问题。只有当我运行服务器使用uwsgi
时,它才能找到_psycopg
模块。
这是我的uwsgi.ini:
[uwsgi]
# variables
base = /var/www/mysite
# general settings
master = True
plugins = python3
vacuum = True
env = DJANGO_SETTINGS_MODULE=config.settings.prod
env = PYTHONHASHSEED=random
# directory containing packages
chdir=%(base)/server
# python modules and paths
home = %(base)/venv
module = config.wsgi:application
pythonpath = %(chdir)
pythonpath = %(home)/lib/python3.5/site-packages
# socket file settings
socket = %(base)/conf/%n.sock
uid = www-data
gid = www-data
chmod-socket = 644
chown-socket = www-data:www-data
# location of log files
logto = /var/log/uwsgi/%n.log
这两种模式有什么区别?我看到的唯一区别是我 运行 manage.py
作为 root,而 uwsgi
运行s 作为用户 www-data
(根据 .ini
) , 但是 运行ning uwsgi
作为 root 给出了同样的错误。
有什么线索吗?
编辑:
在 uwsgi
错误日志中找到线索:
*** Starting uWSGI 2.0.7-debian (64bit) on [Mon Jan 18 22:27:08 2016] ***
compiled with version: 4.9.1 on 25 October 2014 19:17:54
os: Linux-3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt20-1+deb8u2 (2016-01-02)
nodename: averna
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /var/www/mysite/scripts
detected binary path: /usr/bin/uwsgi-core
your processes number limit is 1789
your memory page size is 4096 bytes
detected max file descriptor number: 65536
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /var/www/mysite/conf/uwsgi.sock fd 3
setgid() to 33
setuid() to 33
Python version: 3.4.2 (default, Oct 8 2014, 10:47:48) [GCC 4.9.1]
PEP 405 virtualenv detected: /var/www/mysite/venv
Set PythonHome to /var/www/mysite/venv
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x23b43f0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 145536 bytes (142 KB) for 1 cores
*** Operational MODE: single process ***
added /var/www/mysite/server/ to pythonpath.
added /var/www/mysite/venv/lib/python3.5/site-packages/ to pythonpath.
Traceback (most recent call last):
File "/var/www/mysite/venv/lib/python3.5/site-packages/django/db/backends/postgresql/base.py", line 20, in <module>
import psycopg2 as Database
File "/var/www/mysite/venv/lib/python3.5/site-packages/psycopg2/__init__.py", line 50, in <module>
from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: No module named 'psycopg2._psycopg'
它说 uwsgi
正在使用 Python 版本 3.4.2(系统版本),但我在 virtualenv 中 运行ning 3.5.1。现在我该如何解决这个问题而不必在我的 virtualenv 中降级到 Python 3.4.2?
psycopg2 不适用于 python 3.5
Supports Python versions from 2.5 to 3.4 (Python 2.4 is supported until Psycopg versions 2.4.x).
如果你想使用这个包,你需要降级到 python 3.4
我通过避免 sudo apt-get install uwsgi
安装 uwsgi
和 uwsgi-plugin-python3
解决了这个问题。这导致了为 Python 3.4(也在我的系统上)构建的版本。
相反,我安装了 sudo pip3 install uwsgi
,pip3
是我的 Python 3.5 目录中的那个。这为 Python 3.5 构建了 uwsgi
及其插件。我只需要将生成的 uwsgi
文件符号链接到 /usr/bin
(ln -s <my python 3.5 dir>/bin/uwsgi /usr/bin/uwsgi
),以便它可用 system-wide.
psycopg2
对我来说似乎可以很好地与 Python 3.5 一起使用,所以我唯一的问题是 uwsgi
需要使用 Python 3.5.
当我尝试 运行 uwsgi --ini <my uwsi ini file>
:
Traceback (most recent call last):
File "/var/www/mysite/venv/lib/python3.5/site-packages/django/db/backends/postgresql/base.py", line 20, in <module>
import psycopg2 as Database
File "/var/www/mysite/venv/lib/python3.5/site-packages/psycopg2/__init__.py", line 50, in <module>
from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: No module named 'psycopg2._psycopg'
但是,当我 运行 站点
时,一切 运行 都很好python manage.py runserver --settings=config.settings.prod
psycopg2
已安装,Django 查不到问题。只有当我运行服务器使用uwsgi
时,它才能找到_psycopg
模块。
这是我的uwsgi.ini:
[uwsgi]
# variables
base = /var/www/mysite
# general settings
master = True
plugins = python3
vacuum = True
env = DJANGO_SETTINGS_MODULE=config.settings.prod
env = PYTHONHASHSEED=random
# directory containing packages
chdir=%(base)/server
# python modules and paths
home = %(base)/venv
module = config.wsgi:application
pythonpath = %(chdir)
pythonpath = %(home)/lib/python3.5/site-packages
# socket file settings
socket = %(base)/conf/%n.sock
uid = www-data
gid = www-data
chmod-socket = 644
chown-socket = www-data:www-data
# location of log files
logto = /var/log/uwsgi/%n.log
这两种模式有什么区别?我看到的唯一区别是我 运行 manage.py
作为 root,而 uwsgi
运行s 作为用户 www-data
(根据 .ini
) , 但是 运行ning uwsgi
作为 root 给出了同样的错误。
有什么线索吗?
编辑:
在 uwsgi
错误日志中找到线索:
*** Starting uWSGI 2.0.7-debian (64bit) on [Mon Jan 18 22:27:08 2016] ***
compiled with version: 4.9.1 on 25 October 2014 19:17:54
os: Linux-3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt20-1+deb8u2 (2016-01-02)
nodename: averna
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /var/www/mysite/scripts
detected binary path: /usr/bin/uwsgi-core
your processes number limit is 1789
your memory page size is 4096 bytes
detected max file descriptor number: 65536
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /var/www/mysite/conf/uwsgi.sock fd 3
setgid() to 33
setuid() to 33
Python version: 3.4.2 (default, Oct 8 2014, 10:47:48) [GCC 4.9.1]
PEP 405 virtualenv detected: /var/www/mysite/venv
Set PythonHome to /var/www/mysite/venv
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x23b43f0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 145536 bytes (142 KB) for 1 cores
*** Operational MODE: single process ***
added /var/www/mysite/server/ to pythonpath.
added /var/www/mysite/venv/lib/python3.5/site-packages/ to pythonpath.
Traceback (most recent call last):
File "/var/www/mysite/venv/lib/python3.5/site-packages/django/db/backends/postgresql/base.py", line 20, in <module>
import psycopg2 as Database
File "/var/www/mysite/venv/lib/python3.5/site-packages/psycopg2/__init__.py", line 50, in <module>
from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: No module named 'psycopg2._psycopg'
它说 uwsgi
正在使用 Python 版本 3.4.2(系统版本),但我在 virtualenv 中 运行ning 3.5.1。现在我该如何解决这个问题而不必在我的 virtualenv 中降级到 Python 3.4.2?
psycopg2 不适用于 python 3.5
Supports Python versions from 2.5 to 3.4 (Python 2.4 is supported until Psycopg versions 2.4.x).
如果你想使用这个包,你需要降级到 python 3.4
我通过避免 sudo apt-get install uwsgi
安装 uwsgi
和 uwsgi-plugin-python3
解决了这个问题。这导致了为 Python 3.4(也在我的系统上)构建的版本。
相反,我安装了 sudo pip3 install uwsgi
,pip3
是我的 Python 3.5 目录中的那个。这为 Python 3.5 构建了 uwsgi
及其插件。我只需要将生成的 uwsgi
文件符号链接到 /usr/bin
(ln -s <my python 3.5 dir>/bin/uwsgi /usr/bin/uwsgi
),以便它可用 system-wide.
psycopg2
对我来说似乎可以很好地与 Python 3.5 一起使用,所以我唯一的问题是 uwsgi
需要使用 Python 3.5.