1

I'm getting the following import error when I try to run 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'

However, everything runs just fine when I run the site with

python manage.py runserver --settings=config.settings.prod

psycopg2 is installed, and can be found no problem by Django. Only when I run the server using uwsgi can it not find the _psycopg module.

This is my 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

What can be the difference between these two modes? The only difference I see is that I run manage.py as root, and uwsgi runs as user www-data (as per the .ini), but running uwsgi as root gave the same error.

Any clues?

EDIT:

Found a clue in the uwsgi error log:

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

It says uwsgi is using Python version 3.4.2 (system version), but I'm running 3.5.1 in a virtualenv. Now how do I solve this without having to downgrade to Python 3.4.2 in my virtualenv?

Def_Os
  • 5,301
  • 5
  • 34
  • 63
  • One thing I am noticing is the uwsgi call is running python3.5 whereas, in most circumstances I would expect people to be running python2.7 with Django. Are you sure psycopg2 is installed under your python3.5 version? – Dandy Jan 19 '16 at 06:19
  • @AaronLayfield, everything is based on Python 3.5. Django runs fine with it, it's just this problem with `uwsgi` that's troubling me. – Def_Os Jan 19 '16 at 06:20
  • Alright, let me look a little more into it. Just wanted to cross of the basic. – Dandy Jan 19 '16 at 06:21
  • 1
    What happens when you run the python console and `import psycopg2`? If this works, follow up with `from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID`. If both Django and wsgi are running off the same Python version, there's no logical reason for this to fail other then a potential disparity in version. Do you have more versions of Python3.X installed? – Dandy Jan 19 '16 at 06:27
  • Just edited my question, because I found a clue. It points in the same direction as your insight: Python version problem (3.4 vs 3.5). Console works just fine, BTW. – Def_Os Jan 19 '16 at 06:32
  • 1
    Looks like you may have to reinstall uWSGI or at least rebuild it using `python3.5 uwsgiconfig.py --build` or something along these lines. Technically, this should correctly 'link' uwsgi to the correct version http://projects.unbit.it/uwsgi/wiki/MultiPython ~~ http://stackoverflow.com/questions/13537373/switch-python-version-on-uwsgi (Something asked before). – Dandy Jan 19 '16 at 06:40
  • Looks like it... :( Thanks for the help! – Def_Os Jan 19 '16 at 06:46

2 Answers2

1

psycopg2 doesn't work with python 3.5

From the features list

Supports Python versions from 2.5 to 3.4 (Python 2.4 is supported until Psycopg versions 2.4.x).

You'll need to downgrade to python 3.4 if you wish to use this package

Sayse
  • 42,633
  • 14
  • 77
  • 146
  • Just came back to state this, did a bit of digging in my free time and discovered this. Sorry @Def_Os! – Dandy Jan 19 '16 at 09:43
  • Actually, since I have no problems running the site using Django's `manage.py`, it means that this is not the cause of my problems and `psycopg2` works with Python 3.5 for me. – Def_Os Jan 20 '16 at 03:17
  • 1
    This is not true as of 2019. – Uri Aug 12 '19 at 04:34
1

I solved this by avoiding sudo apt-get install uwsgi to install uwsgi and the uwsgi-plugin-python3. This resulted in versions built for Python 3.4 (which is also on my system).

Instead, I installed with sudo pip3 install uwsgi, with the pip3 being the one from my Python 3.5 directory. This built uwsgi and its plugins for Python 3.5. I just had to symlink the resulting uwsgi file to /usr/bin (ln -s <my python 3.5 dir>/bin/uwsgi /usr/bin/uwsgi), so that it was available system-wide.

psycopg2 seems to work fine with Python 3.5 for me, so my only problem was that uwsgi needed to be built using Python 3.5.

Def_Os
  • 5,301
  • 5
  • 34
  • 63