【发布时间】:2017-07-24 16:07:08
【问题描述】:
我将我的 Django 项目从 Python 2.7 移到了 Python 3.6。
从那时起,我无法启动和运行我的项目,因为 Apache 抱怨如下(重启后的完整输出):
[mpm_prefork:notice] [pid 2340] AH00173: SIGHUP received. Attempting to restart
[:notice] [pid 25019] FastCGI: process manager initialized (pid 25019)
[:warn] [pid 2340] mod_wsgi: Compiled for Python/3.4.0.
[:warn] [pid 2340] mod_wsgi: Runtime using Python/3.4.3.
[mpm_prefork:notice] [pid 2340] AH00163: Apache/2.4.7 (Ubuntu) mod_fastcgi/mod_fastcgi-SNAP-0910052141 PHP/5.5.9-1ubuntu4.21 OpenSSL/1.0.1f mod_wsgi/3.4 Python/3.4.3 configured -- resuming normal operations
[core:notice] [pid 2340] AH00094: Command line: '/usr/sbin/apache2'
[:error] [pid 25025] [client 10.0.10.117:49933] mod_wsgi (pid=25025): Target WSGI script '/home/myuser/projects/myproject/myproject_project/wsgi.py' cannot be loaded as Python module.
[:error] [pid 25025] [client 10.0.10.117:49933] mod_wsgi (pid=25025): Exception occurred processing WSGI script '/home/myuser/projects/myproject/myproject_project/wsgi.py'.
[:error] [pid 25025] [client 10.0.10.117:49933] Traceback (most recent call last):
[:error] [pid 25025] [client 10.0.10.117:49933] File "/home/myuser/projects/myproject/myproject_project/wsgi.py", line 29, in <module>
[:error] [pid 25025] [client 10.0.10.117:49933] application = get_wsgi_application()
[:error] [pid 25025] [client 10.0.10.117:49933] File "/home/myuser/.virtualenvs/myproject-3.6/lib/python3.6/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
[:error] [pid 25025] [client 10.0.10.117:49933] django.setup(set_prefix=False)
[:error] [pid 25025] [client 10.0.10.117:49933] File "/home/myuser/.virtualenvs/myproject-3.6/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
[:error] [pid 25025] [client 10.0.10.117:49933] apps.populate(settings.INSTALLED_APPS)
[:error] [pid 25025] [client 10.0.10.117:49933] File "/home/myuser/.virtualenvs/myproject-3.6/lib/python3.6/site-packages/django/apps/registry.py", line 85, in populate
[:error] [pid 25025] [client 10.0.10.117:49933] app_config = AppConfig.create(entry)
[:error] [pid 25025] [client 10.0.10.117:49933] File "/home/myuser/.virtualenvs/myproject-3.6/lib/python3.6/site-packages/django/apps/config.py", line 116, in create
[:error] [pid 25025] [client 10.0.10.117:49933] mod = import_module(mod_path)
[:error] [pid 25025] [client 10.0.10.117:49933] File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module
[:error] [pid 25025] [client 10.0.10.117:49933] return _bootstrap._gcd_import(name[level:], package, level)
[:error] [pid 25025] [client 10.0.10.117:49933] File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
[:error] [pid 25025] [client 10.0.10.117:49933] File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
[:error] [pid 25025] [client 10.0.10.117:49933] File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
[:error] [pid 25025] [client 10.0.10.117:49933] File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
[:error] [pid 25025] [client 10.0.10.117:49933] File "<frozen importlib._bootstrap>", line 1129, in _exec
[:error] [pid 25025] [client 10.0.10.117:49933] File "<frozen importlib._bootstrap>", line 1471, in exec_module
[:error] [pid 25025] [client 10.0.10.117:49933] File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
[:error] [pid 25025] [client 10.0.10.117:49933] File "/home/myuser/.virtualenvs/myproject-3.6/lib/python3.6/site-packages/django/contrib/postgres/apps.py", line 7, in <module>
[:error] [pid 25025] [client 10.0.10.117:49933] from .signals import register_hstore_handler
[:error] [pid 25025] [client 10.0.10.117:49933] File "/home/myuser/.virtualenvs/myproject-3.6/lib/python3.6/site-packages/django/contrib/postgres/signals.py", line 1, in <module>
[:error] [pid 25025] [client 10.0.10.117:49933] from psycopg2 import ProgrammingError
[:error] [pid 25025] [client 10.0.10.117:49933] File "/home/myuser/.virtualenvs/myproject-3.6/lib/python3.6/site-packages/psycopg2/__init__.py", line 50, in <module>
[:error] [pid 25025] [client 10.0.10.117:49933] from psycopg2._psycopg import ( # noqa
[:error] [pid 25025] [client 10.0.10.117:49933] ImportError: No module named 'psycopg2._psycopg'
但是,当我尝试在 virtualenv 中手动加载问题模块 (psycopg2) 时,完全可以这样做:
(myproject-3.6)myuser@Server:$ python
Python 3.6.0 (default, Jan 13 2017, 00:00:00)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>>
我正在使用 WSGI。其文件内容如下:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
我的主要 Apache2 站点配置如下(也有一个 admin/ 部分):
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /home/myuser/projects/myproject/myproject_project/wsgi.py
#WSGIPythonPath /home/myuser/projects/myproject:/home/myuser/.virtualenvs/myproject/local/lib/python2.7/site-packages
WSGIPythonPath /home/myuser/projects/myproject:/home/myuser/.virtualenvs/myproject-3.6/lib/python3.6/site-packages
<Directory /home/myuser/projects/myproject/myproject_project>
SSLRequireSSL
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>
我已经通过apt-get 安装了python3-psycopg2 包。当然,我的 virtualenv 也安装了 psycopg2:
(myproject-3.6)myuser@Server:~/projects/$ pip freeze | grep psy
psycopg2==2.7
出了什么问题?
我使用的是 Ubuntu 14.04 LTS。
【问题讨论】:
-
请阅读modwsgi.readthedocs.io/en/develop/user-guides/… 文档中的警告,其中说您不能强制为一个 Python 版本编译的 mod_wsgi 使用基于不同 Python 版本的 Python 虚拟环境。换句话说,您必须为 Python 3.6 安装 mod_wsgi。您的 mod_wsgi 是为 Python 3.4 构建的。该文档还告诉您如何正确指定要使用的虚拟环境。你怎么做不是推荐的方式。
-
谢谢;将我的 venv 降级到 Python 3.4 解决了这个问题。
-
强烈推荐:迁移到 nginx+gunicorn
标签: django apache python-3.x ubuntu-14.04 virtualenv