如果我使用manage.py
runserver,一切都运行得很好但是我使用 apache + mod_wsgi 部署的那一刻我得到了
ImportError: datetime initialization failed.
环境详情
CentOS 6 Apache/2.4.6 Python 3.5.1 on virtualenv postgresql-libs-8.4.20-4.el6_7.x86_64
Python模块
Django==1.9.1
mod-wsgi==4.4.22
mod-wsgi-httpd==2.4.12.6
psycopg2==2.6.1
wheel==0.26.0
错误记录
[wsgi:warn] [pid 7826] mod_wsgi: Compiled for Python/3.5.1.
[wsgi:warn] [pid 7826] mod_wsgi: Runtime using Python/3.5.0.
虚拟主机日志
Loading WSGI script '/home/mkt/src/market/wsgi.py'. [wsgi:error] [pid 7828] mod_wsgi (pid=7828): Target WSGI script '/home/mkt/src/market/wsgi.py' cannot be loaded as Python module. [wsgi:error] [pid 7828] mod_wsgi (pid=7828): Exception occurred processing WSGI script '/home/mkt/src/market/wsgi.py'. [wsgi:error] [pid 7828] Traceback (most recent call last): [wsgi:error] [pid 7828] File "/home/mkt/mktve/lib/python3.5/site-packages/django/db/backends/postgresql/base.py", line 20, in [wsgi:error] [pid 7828] import psycopg2 as Database [wsgi:error] [pid 7828] File "/home/mkt/mktve/lib/python3.5/site-packages/psycopg2/__init__.py", line 50, in [wsgi:error] [pid 7828] from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID [wsgi:error] [pid 7828] ImportError: datetime initialization failed [wsgi:error] [pid 7828]
Apache配置
Alias /static /home/mkt/src/market/static
WSGIScriptAlias / /home/mkt/src/market/wsgi.py
WSGIDaemonProcess mysite.com python-path=/home/mkt/src:/home/mkt/mktve/lib/python3.5/site-packages user=myuser group=myuser
WSGIProcessGroup mysite.com
<Directory /home/mkt/src/market/static>
Require all granted
#Allow from All
</Directory>
<Directory /home/mkt/src/market>
<Files wsgi.py>
Require all granted
#Satisfy Any
#Allow from All
</Files>
</Directory>
LogLevel info
<Directory /home/mkt/src/market/static>
Require all granted
#Allow from All
</Directory>
<Directory /home/mkt/src/market>
<Files wsgi.py>
Require all granted
#Satisfy Any
#Allow from All
</Files>
</Directory>
LogLevel info
LDD
注意:我猜测问题出在 psycopg2 + mod_wsgi 上,因为运行 sqlite 的同一台服务器上的另一个虚拟主机工作正常。