当我尝试运行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
这两种模式有什么区别?我看到的唯一区别是我以root身份运行manage.py
,uwsgi
以用户www-data
运行(根据.ini
),但正在运行uwsgi
as 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中运行3.5.1。现在我如何解决这个问题而不必在我的virtualenv中降级到Python 3.4.2?
答案 0 :(得分:2)
psycopg2不适用于python 3.5
支持2.5到3.4的Python版本(支持Python 2.4直到Psycopg版本2.4.x)。
如果你想使用这个软件包,你需要降级到python 3.4
答案 1 :(得分:1)
我通过避免sudo apt-get install uwsgi
安装uwsgi
和uwsgi-plugin-python3
来解决这个问题。这导致为Python 3.4构建的版本(也在我的系统上)。
相反,我使用sudo pip3 install uwsgi
安装,pip3
是我的Python 3.5目录中的uwsgi
。这构建了uwsgi
及其Python 3.5的插件。我只需将生成的/usr/bin
文件符号链接到ln -s <my python 3.5 dir>/bin/uwsgi /usr/bin/uwsgi
(psycopg2
),以便系统范围内可用。
uwsgi
似乎可以正常使用Python 3.5,所以我唯一的问题是需要使用Python 3.5构建public void sendAllMails(List<String> mailIds,String subject,String text)
{
String smtpHost="127.0.0.1";
String fromAddress = "user1@mymail.com";
String mailUser = "user1";
String password = "pass1";
String mailId=null;
try{
java.util.Properties properties = System.getProperties();
Session session = Session.getInstance(properties, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromAddress));
Iterator<String> iterator=mailIds.iterator();
while(iterator.hasNext()){
mailId=iterator.next();
message.addRecipients(Message.RecipientType.BCC, mailId);
}
message.setSubject(subject);
message.setContent(text, "text/html");
session.setDebug(true);
Transport transport = session.getTransport("smtp");
transport.connect(smtpHost, mailUser, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch(Exception e){
e.printStackTrace();
}
}
。