6

Hi I am getting the error below when going to the website url on ubuntu server 14.10 running apache 2 with mod_wsgi and python on django.

My django application uses python 3.4 but it seems to be defaulting to python 2.7, I am unable to import image from PIL and AES from pycrypto.

ImportError at /
cannot import name _imaging
Request Method: GET
Request URL:
Django Version: 1.7.3
Exception Type: ImportError
Exception Value:
cannot import name _imaging
Exception Location: /usr/local/lib/python3.4/dist-packages/PIL/Image.py in , line 63
Python Executable: /usr/bin/python
Python Version: 2.7.6
Python Path:
['/var/www/blabla',
'/usr/local/lib/python3.4/dist-packages',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/var/www/blabla',
'/usr/local/lib/python3.4/dist-packages']

1
  • 1
    Yep. No doubt about it using Python 2.7. How have you configured the Apache site to use mod_wsgi? Can you post your site configuration? Commented Jan 19, 2015 at 7:21

3 Answers 3

9

I believe that mod_wsgi is compiled against a specific version of python, so you need a py3.4 version of mod_wsgi. You may be able to get one from your os's package repository or you can build one without too much drama. From memory you'll need gcc and python-dev packages (python3-dev?) to build.

OK, quick google, for ubuntu 14.10: sudo apt-get install libapache2-mod-wsgi-py3 should install a py3 version of mod_wsgi (will probably want to remove the existing py2 version).

Adding a shebang line won't do any good as the python interpreter is already loaded before the wsgi.py script is read.

Sign up to request clarification or add additional context in comments.

1 Comment

Lifesaver answer. I spent 4 hours trying to figure this out.
0

From what I see here your application is using py2 interpreter with py3 compiled modules, which is no-go.

One simple possible solution that comes me in mind is to add or change first line of manage.py to #!/usr/bin/python3. This will tell script to be interpreted with py3.

Next on guess list would be misconfiguration in *.wsgi file or apache config, whichever you are using.

2 Comments

Apache Config: DocumentRoot /var/www/html WSGIDaemonProcess sampleapp python-path=/project folder/MITLogger:/usr/local/lib/python3.4/dist-packages WSGIProcessGroup sampleapp WSGIScriptAlias / /project folder/MITLogger/MITLogger/wsgi.py <Directory /project folder/MITLogger/MITLogger> <Files wsgi.py> Require all granted </Files> </Directory>
wsgi.py: import os, sys # add the hellodjango project path into the sys.path sys.path.append('/project folder/MITLogger') # add the virtualenv site-packages path to the sys.path sys.path.append('/usr/local/lib/python3.4/dist-packages') os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MITLogger.settings") from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
0

Thanks guys,

I actually fixed the issue myself this morning by running the make install of mod_wsgi with .configure pointing to python3.4.

I think you were right Adam.

2 Comments

Yeah, I'm pretty sure just changing WSGIPythonHome doesn't work. It is useful to select a virtualenv though. Mark my answer as correct if you deem it so :-)
To clear something up - there was another answer here suggesting to just change WSGIPythonHome. WSGIPythonHome will choose the interpreter but will only work when mod_wsgi is compiled against the same major/minor version of python that you are pointing to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.