Apache无法正确加载virtualenv:" ImportError:没有名为' psycopg2._psycopg'"

时间:2017-03-04 15:20:52

标签: django apache python-3.x ubuntu-14.04 virtualenv

我将我的Django项目从Python 2.7移到了Python 3.6。

从那以后,我无法完成我的项目。运行,因为Apache抱怨如下(重启后的完整输出):

compile 'commons-io:commons-io:2.4'

但是当我尝试手动加载virtualenv中的问题模块(psycopg2)时,完全可以这样做:

[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'

我正在使用WSGI。其文件内容如下:

(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
>>>

我的主要Apache2站点配置如下(还有一个admin /部分):

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

我已通过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> 安装了python3-psycopg2个软件包。当然我的virtualenv也安装了psycopg2:

apt-get

出了什么问题?

我使用的是Ubuntu 14.04 LTS。

1 个答案:

答案 0 :(得分:1)

我在Debian 8机器上使用Python 3.6,Apache2.4和WSGI运行我的Django 1.11应用程序时遇到了同样的问题。

问题是libapache2-mod-wsgi-py针对apache2python的特定版本进行了编译,并且与不同的python版本不兼容。 当然,

  • libapache2-mod-wsgi-py用于python 2.7,
  • libapache2-mod-wsgi-py3 ---对于python3,

但是小的python版本也很重要。

我们来看看。在@SaeX堆栈跟踪中,大多数行是关于 Python 3.6 venv的

...
File "/home/myuser/.virtualenvs/myproject-3.6/lib/python3.6/site-packages/django/apps/config.py", line 116, in create
...

但是有一行 Python 3.4 调用

...
File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module
...

我认为在他的操作系统存储库中(当时),python3提供的最新apt-get install python3版本为3.4.n,而apt-get install libapache2-mod-wsgi-py3的结果为mod_wsgi为Python 3.4编译。这就是原因。

在我的情况下,我首先在堆栈跟踪中找到了python2.7

File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module'

这意味着我尝试使用libapache2-mod-wsgi-py代替libapache2-mod-wsgi-py3。但是当我安装libapache2-mod-wsgi-py3时,我在堆栈跟踪中得到python3.5

File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module'

我已经用两种方式解决了这个问题,对我来说都没问题:

  1. 使用操作系统软件包存储库中的最新python3版本;
  2. mod-wsgi
  3. 构建python3.6

    <强> 1。最新系统Python 3

    @SaeX使用这种方式:

      

    感谢;将我的venv降级为Python 3.4修复了问题

    在您的情况下,最新的Python 3版本可能会有所不同。

    apt-get update
    apt-get install python3 python3-venv
    
    # check python3 version
    python3 -V
    # >>> Python 3.5.4 is for my Debian 8 in September 2017
    
    # setting virtual environment for WSGI
    python3 -m venv /home/pyvenv_354
    source /home/pyvenv_354/bin/activate
    # check venv and get path for WSGI
    (pyvenv_354) python -c 'import sys; print(sys.prefix)'
    # >>> /home/pyvenv_354
    
    # mod_wsgi from repo must be compatible
    apt install libapache2-mod-wsgi-py3
    

    现在可以在python3.5

    中使用此apache2 venv
    # sites-available/mysite.conf
    ...
    Define WWW_ROOT /var/www/mysite/mysite
    ...
    Define PYTHON_VENV_HOME /home/pyvenv_354
    Define PYTHON_VENV_PACKAGES ${PYTHON_VENV_HOME}/lib/python3.5/site-packages
    WSGIDaemonProcess mysite processes=2 python-path=${WWW_ROOT}:${PYTHON_VENV_PACKAGES}
    WSGIProcessGroup mysite
    WSGIScriptAlias / ${MAIN_APP_PATH}/wsgi.py
    

    最后使用您喜欢的方式重启Apache2,例如

    service apache2 restart
    

    <强> 2。为Python 3.6构建mod_wsgi

    我用mod_wsgi quick installation guide来执行它。 注意: apache2-devpython-dev个套餐是必需的。

    apt-get install python3.6 python3.6-dev apache2-dev
    # checking Python version
    python3.6 -V
    # >>> 3.6.2
    
    # set virtual environment
    python3.6 -m venv /home/pyvenv_362
    
    # download mod_wsgi sources from https://github.com/GrahamDumpleton/mod_wsgi/releases
    # x, y, z --- version, select using link above
    cd /usr/src/
    wget https://github.com/GrahamDumpleton/mod_wsgi/archive/x.y.z.tar.gz
    tar xvfz x.y.z.tar.gz
    cd mod_wsgi-x.y.z
    
    # set python3.6 venv for mod_wsgi
    ./configure --with-python=/home/pyvenv_362/bin/python
    
    make
    make install
    
    # >>> Libraries have been installed in:
    #   /usr/lib/apache2/modules
    #   mod_wsgi.so
    
    # adding module to apache2
    cat /etc/apache2/mods-enabled/wsgi.load
    nano /etc/apache2/httpd.conf
    add to httpd.conf line `LoadModule wsgi_module modules/mod_wsgi.so`
    

    现在可以在python3.6

    中使用apache2 venv
    # sites-available/mysite.conf
    ...
    Define WWW_ROOT /var/www/mysite/mysite
    ...
    Define PYTHON_VENV_HOME /home/pyvenv_362
    Define PYTHON_VENV_PACKAGES ${PYTHON_VENV_HOME}/lib/python3.6/site-packages
    WSGIDaemonProcess mysite processes=2 python-path=${WWW_ROOT}:${PYTHON_VENV_PACKAGES}
    WSGIProcessGroup mysite
    WSGIScriptAlias / ${MAIN_APP_PATH}/wsgi.py
    

    最后使用您喜欢的方式重启Apache2,例如

    service apache2 restart