33

I installed Django with this command:

sudo apt-get install python-django

And it installed version 1.5.4-1ubuntu1. The Django docs say that as of version 1.5, Django supports Python 3. Yet when I run the python 3 interpreter and try importing Django, it says there's no such module. I tried looking for a python3 version of Django, but no such package exists. How can I get django working on python3? I'd prefer to use ubuntu packages instead of installing things manually.

1

4 Answers 4

38

I would strongly suggest avoiding pip3 to install things in the system site-packages. I've made these arguments before but I'll give you the notes:

  • System updates break everything
  • Installing apt packages can overwrite pip-installed things
  • Version conflicts
  • Distribution upgrades are unpredictable chaos. Seriously. I've lost hair to these.

I would strongly advocate using virtualenv. It's a massive pain in the wherever to get going but once you've got it set up you have a complete Python environment under your complete control. This does mean more work (you'll have to check things for updates and pip doesn't really help there yet) but you don't have to worry about what Ubuntu's doing.

I have an environment for each site (just sitting in a subdirectory called venv). Some people —including me, once upon a time— prefer to share their environments between multiple sites. I found this easier to maintain in terms of issuing updates but some sites can be fragile or require old versions and that holds the whole environment back. YMMV.

In terms of installing this, and just to slap me in the face, virtualenv isn't packaged for Python 3 yet so we have to use pip3:

$ sudo pip3 install virtualenv
...

$ virtualenv-3.3 myenv
Using base prefix '/usr'
New python executable in myenv/bin/python3
Also creating executable in myenv/bin/python
Installing setuptools, pip...done.

$ source myenv/bin/activate  # This is important!

Your bash PS1 should now be preneded with (myenv) to let you know you're in a different environment. We can test the environment to check we're on the right versions of things (not using the system versions for starters):

$ python --version
Python 3.3.2+
$ which python pip 
/home/oli/Desktop/myenv/bin/python
/home/oli/Desktop/myenv/bin/pip

And then you can just carry on as if you were master of the universe. You don't need root to use pip anymore and you don't need to specify pip3. It's just a lot more friendly.

$ pip install django umemcache
...

If you're using something like uwsgi to host this (you should) use its -H flag (or home config argument) to tell it where the Python environment lives.

As for making development easier, you can automatically "mount" your virtualenv environment. There are many scripts out there but this one is mine (this lives at the bottom of my ~/.bashrc:

export VENVDIR="/web"
export VENVDIR_VENV="$VENVDIR/venv"

venvcd() {
        wd=$(pwd)
        [[ $wd == $VENVDIR/* || $wd == $VENVDIR ]] && wasin=true || unset wasin

        builtin cd "$@"

        wd=$(pwd)
        if [[ $wd == $VENVDIR/* || $wd == $VENVDIR ]]; then
                source $VENVDIR_VENV/bin/activate
        else
                [ $wasin ] && deactivate
        fi
}
alias cd="venvcd"

cd .

Whenever I cd into /web (where all my development websites are stored), it mounts the virtualenv for me. Note that I only have one environment for all my sites so this will only suite you if you do something similar. There are many other ways of doing similar things.

18

I figured out how to do this with pip. Turns out I needed pip3:

sudo pip3 install Django

did the trick.

You might need to get pip3 by:

sudo apt-get install python3-pip
4

Django for Python3 has now been added recently. So simply type in a terminal:

apt-get install python3-django

4
  • 1
    E: Unable to locate package python3-django
    – Jonathan
    Aug 29, 2014 at 14:06
  • 1
    It's not available for Ubuntu 14.04 Trusty Tahr.
    – Anwar
    Feb 28, 2015 at 18:22
  • 1
    I don't suggest using the system-repos django. If there will be an update, some of the functionallity you are using might not work after the update. Better use the virtual-env approach as shown above.
    – 0xAffe
    Oct 27, 2015 at 8:38
  • I'm on the latest Debian (8, up-to-date), and Django 1.7.11 (which is EOL!) is the latest version available in the repositories. It looks like Ubuntu has a supported version (1.8.7.) I wonder why Debian is so far behind?
    – Hydraxan14
    Dec 29, 2016 at 17:05
3

Guys there is an easy way using virtualenvwrapper, by far the most easiest to manage Python 2.7/3.x virtual environments in Ubuntu/Debian.

$ sudo apt-get install python3 virtualenvwrapper
$ mkvirtualenv <venv> -p python3
$ workon <venv> # sets into the virtualenv from any terminal.

Optionally enable --system-site-packages to reuse global site packages.
virtualenv installs pip and other python setup tools to help us get started quickly.

$ pip install Django
$ pip install -r requirements.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .