1

Everything runs perfectly if I use manage.py runserver but the moment I deploy using apache+mod_wsgi i get

ImportError: datetime initialization failed.

Environment details

    CentOS 6
    Apache/2.4.6 
    Python 3.5.1 on virtualenv
    postgresql-libs-8.4.20-4.el6_7.x86_64

Python Modules

Django==1.9.1
mod-wsgi==4.4.22
mod-wsgi-httpd==2.4.12.6
psycopg2==2.6.1
wheel==0.26.0

Error Log

[wsgi:warn] [pid 7826] mod_wsgi: Compiled for Python/3.5.1.
[wsgi:warn] [pid 7826] mod_wsgi: Runtime using Python/3.5.0.

Virtual Host Log

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

LDD

linux-vdso.so.1 =>  (0x00007ffc7bd42000)
libpython3.5m.so.1.0 => /usr/lib/libpython3.5m.so.1.0 (0x00007f2e42451000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f2e42234000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f2e4202f000)
libutil.so.1 => /lib64/libutil.so.1 (0x00007f2e41e2c000)
librt.so.1 => /lib64/librt.so.1 (0x00007f2e41c24000)
libm.so.6 => /lib64/libm.so.6 (0x00007f2e4199f000)
libc.so.6 => /lib64/libc.so.6 (0x00007f2e4160b000)
/lib64/ld-linux-x86-64.so.2 (0x00007f2e42b83000)

Note: I'm guessing that the problem is with psycopg2+mod_wsgi because another virtualhost on the same server running sqlite is working fine.

doru
  • 9,022
  • 2
  • 33
  • 43
kwatog
  • 33
  • 6
  • Is it only `mod-wsgi` that gives you this error? Can you run migrations and access the python shell with `manage.py shell`? – Håken Lid Feb 06 '16 at 08:21
  • The exception is raised in `psycopg2/__init__.py`. It could be some internal bug with psycopg2. You could try submitting an issue on github. https://github.com/psycopg/psycopg2 – Håken Lid Feb 06 '16 at 08:25
  • 1
    You may want to try running your app with gunicorn to test your wsgi with another server before filling a bug: http://gunicorn.org/ – n3storm Feb 06 '16 at 10:00
  • Yes, I can run migration. Everything is fine if I use manage.py. I'll check on gunicorn.org. – kwatog Feb 06 '16 at 12:47
  • by the way before posting the question here, I saw a somewhat similar bug in psycopg2 but was flagged as problem of mod_wsgi. – kwatog Feb 06 '16 at 12:49
  • I ran `python manage.py runmodwgi 0.0.0.0:8000` and it resulted to the same error. of course, `python manage.py runserver 0.0.0.0:8000` works perfectly fine. – kwatog Feb 07 '16 at 14:32
  • @n3storm, it works with gunicorn. thanks for recommending that. I put it behind apache, though. probably for the time being, this will be my setup until mod_wsgi is updated. – kwatog Feb 12 '16 at 01:47
  • 1
    @kwatog you are welcome. Just to let you know having the http server and application server so tight as in apache wsgi or apache php is not the best way to handle deployment, load balance, application evolution, ... I think you are in the right path now and I bet you will keep using gunicorn and maybe move to nginx if you have many users ;) Cheers – n3storm Feb 12 '16 at 07:05

0 Answers0