No module named 'psycopg2._psycopg'

2 700 vues
Accéder directement au premier message non lu

Quentin Caron

non lue,
25 févr. 2016, 06:32:0825/02/2016
à modwsgi
Hi guys!

I have open a thread on Stack Overflow about my issue but I believe you will be able to help me :) Here is my problem:

I am struggling in making Django run with mod_wsgi. I tried to follow recommendations here but it did not work.

The server is a Debian :

$ lsb_release -da
    No LSB modules are available.
    Distributor ID: Debian
    Description:    Debian GNU/Linux 8.3 (jessie)
    Release:    8.3
    Codename:   jessie

Python 3.5.1 has been installed with --ensable-shared. The virtualenv venv uses this Python because of the following in the user .bash_profile :

LD_LIBRARY_PATH=/usr/local/lib
VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3.5
WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

libapache2-mod-wsgi-py3 has been installed, as it is explained, it works for Python3.X. So why do I have a "No module named 'psycopg2._psycopg'" error here ?

mod_wsgi has been installed in the virtualenv venv.

Here is the virtualhost:

<VirtualHost *:80>
    ServerName mysite.com
    ServerAlias mysite.com
    ServerAdmin webmaster@mysite.com
    DocumentRoot /var/www/mysite.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    WSGIDaemonProcess api python-path=/var/www/mysite.com:/home/user/.virtualenvs/venv/lib/python3.5/site-packages
    WSGIProcessGroup api
    WSGIScriptAlias / /var/www/mysite.com/wsgi.py

    <Directory /var/www/mysite.com>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
</VirtualHost>

Here is the content of my apache error.log file:

mod_wsgi: Compiled for Python/3.4.2rc1+.
mod_wsgi: Runtime using Python/3.4.2.
AH00489: Apache/2.4.10 (Debian) mod_wsgi/4.3.0 Python/3.4.2 configured -- resuming normal operations
AH00094: Command line: '/usr/sbin/apache2'
mod_wsgi (pid=12796): Target WSGI script '/var/www/mysite.com/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=12796): Exception occurred processing WSGI script '/var/www/mysite.com/wsgi.py'.
Traceback (most recent call last):
File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/db/backends/postgresql/base.py", line 20, in <module>
import psycopg2 as Database
File "/home/user/.virtualenvs/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'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/var/www/mysite.com/wsgi.py", line 18, in <module>
application = get_wsgi_application()
File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
django.setup()
File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/contrib/auth/models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
class AbstractBaseUser(models.Model):
   File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/db/models/base.py", line 108, in __new__
     new_class.add_to_class('_meta', Options(meta, app_label))
   File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/db/models/base.py", line 299, in add_to_class
     value.contribute_to_class(cls, name)
   File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/db/models/options.py", line 263, in contribute_to_class
     self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
   File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/db/__init__.py", line 36, in __getattr__
     return getattr(connections[DEFAULT_DB_ALIAS], item)
   File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/db/utils.py", line 212, in __getitem__
     backend = load_backend(db['ENGINE'])
   File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/db/utils.py", line 116, in load_backend
     return import_module('%s.base' % backend_name)
   File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module
     return _bootstrap._gcd_import(name[level:], package, level)
   File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/contrib/gis/db/backends/postgis/base.py", line 2, in <module>
     from django.db.backends.postgresql.base import \\
   File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/db/backends/postgresql/base.py", line 24, in <module>
     raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
     django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2._psycopg'


Does "mod_wsgi: Runtime using Python/3.4.2." means something has to be compiled with my Python 3.5? Is it a freeze problem?


I can successfully run the following in my venv command interpreter:

from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID

Thanks for your help :)


Graham Dumpleton

non lue,
25 févr. 2016, 06:46:5225/02/2016
à mod...@googlegroups.com
You will need to uninstall mod_wsgi from your Apache installation and recompile/reinstall mod_wsgi from source code so it is compiled against Python 3.5.

You cannot mix Python versions like you are doing by telling mod_wsgi compiled with Python 3.4 to use Python virtual environment directory of a Python 3.5 version.

BTW. Don’t use python-path to refer to site-packages directory. Better to use python-home option to refer to the root directory for the Python virtual environment.


Solve your version mismatch problem first.

Graham

--
You received this message because you are subscribed to the Google Groups "modwsgi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modwsgi+u...@googlegroups.com.
To post to this group, send email to mod...@googlegroups.com.
Visit this group at https://groups.google.com/group/modwsgi.
For more options, visit https://groups.google.com/d/optout.

Quentin Caron

non lue,
25 févr. 2016, 11:52:3325/02/2016
à modwsgi
Hi Graham!

I uninstalled mod_wsgi in my virtualenv and used the mod_wsgi sources to manually install them. Configuring mod_wsgi with Python 3.5 did the job and the URL is now successfully served by Apache. Thank you very much for your help!!
...

Graham Dumpleton

non lue,
25 févr. 2016, 15:36:1625/02/2016
à mod...@googlegroups.com
If you had a mod_wsgi version installed in your Python virtual environment, that wouldn’t have been used. The mod_wsgi-express version you can install using pip usually isn't used if you are using Apache directly and configuring it yourself. It is only the mod_wsgi which was installed into your main Apache installation that is used. That would often be installed using system packages, but since system packages are often out of date or use wrong Python version, you would have to install from source.

Anyway, may just be how you explained it that was a little confusing. That you got it working is the important bit. :-)

Graham

Répondre à tous
Répondre à l'auteur
Transférer
0 nouveau message