How to use Python 3 and Django with Apache? How to use Python 3 and Django with Apache? django django

How to use Python 3 and Django with Apache?


Django 1.6+ and mod_wsgi 3.4+ are required to use Python 3 with Apache. For more detail refer to scot's answer.


These answers are no longer true of Django 1.6 - it supports python3. The mod_wsgi page says version 3.4 supports python 3. https://code.google.com/p/modwsgi/

Don't know if it all works at this point though (I will return and edit when I find out)!

The answer is YES it works!

I have an AWS EC3 Ubuntu instance running Python3, Django 1.5.6, Apache2.2 and mod_wsgi 3.4

Python 3.3.4:

sudo add-apt-repository ppa:fkrull/deadsnakessudo apt-get install python3.3sudo apt-get install python3.3-dev python3.3-doc idle-python3.3

ppa:fkrull/deadsnakes is a apt repo has has multiple python versions available - see https://launchpad.net/~fkrull/+archive/deadsnakes

Then I added pip using the instructions at the pip page; http://pip.readthedocs.org/en/latest/installing.html. (remember your python is probably on your path as 'python3.3' at this point, plain 'python' will point at python 2.x!)

After that, virtualenv. Then I virtualenv'd the python installation. Upon activation and adding the environment's bin/ directory to the $PATH I've now got a clean python3.

Then, after I activated the virtual env, I did 'pip Django' and all my other necessary packages (which were quite a few). I have Django version 1.6.2 (I've been developing on this and running under python 3.3.3 on my Mac no problem).

The most trouble I had was installing lxml because it requires libxml2 and libxslt to be installed with apt-get (it is a wrapper around the C code) and it took me a couple of attempts to realise that they were not already installed (lxml compilation fails).

After tossing about getting my RDS database instance up and running and available (postgresql, beware mysql under python3, you'll get plenty of python db driver pain! but most of my issues were caused by me trying to understand the AWS security configuration), it was relatively plain sailing:

sudo apt-get install apache2 apache2-threaded-dev

That installs apache - and you need the dev packages for the next bit.

And that point, I tried using the apt package for mod_wsgi but I decided that the best thing to do was to compile and install it myself, following the instructions here - https://code.google.com/p/modwsgi/wiki/InstallationInstructions

I had no problems with configure, make, or make install. Make sure you compile it in your virtualenv activated environment.

You have to manually add the configuration to Apache's configuration:

# wsgi moduleLoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so# now configure it<Directory /my/app/path> <Files wsgi.py>  Order deny,allow  Allow from all </Files></Directory>WSGIScriptAlias / /my/app/path/wsgi.pyWSGIPythonPath /my/app:/path/to/the/virtual/env/lib/python3.3/site-packages

And in the broadest possible way, this is all now working.


You can't unser python3 with django. From Django FAQ:http://docs.djangoproject.com/en/dev/faq/install/#can-i-use-django-with-python-3

Can I use Django with Python 3?

Not at the moment. Python 3.0 introduced a number of backwards-incompatible changes to the Python language, and although these changes are generally a good thing for Python’s future, it will be a while before most Python software catches up and is able to run on Python 3.0. For larger Python-based software like Django, the transition is expected to take at least a year or two (since it involves dropping support for older Python releases and so must be done gradually).

In the meantime, Python 2.x releases will be supported and provided with bug fixes and security updates by the Python development team, so continuing to use a Python 2.x release during the transition should not present any risk.