ubuntu - tips & tricks

install python pip and django on ubuntu





Django is a free and open source web application framework, written in Python, which follows the Model–View–Controller (MVC) architectural pattern.  In this article I will provide you instructions to install python pip and django on ubuntu linux to create a development site .

Install  python package management system

pip is a package management system used to install and manage software packages written in Python. Pip is a replacement for easy_install, and is intended to be an improved Python package installer. It is an easy to use command-line interface which makes installing python package as easy as easy as issuing one command:

To install pip open a terminal window type in:

Install python virtual environment

What is VirtualEnv

Virtualenv is a very simple tool, which enables you to creating virtual python environments. It does not enable you to create full virtual environments,  where you can simulate entire production environments,  it only allows you to have separate python executable file, along with a separate set of libraries.

Virtualenv allows you to control which packages are used on a particular project by cloning your main Python. This helps you to to ensure you never install a python package globally, which my result in library conflicts etc. Working inside virtual environments ensures you only install project dependencies inside the projects that require them. Projects with conflicting dependencies can happily co-exist on the same machine.




Create directory for python projects
I usually create a directory on my computer for my python projects. You can name this directory anything you like, and can literally place it where ever you like. However, I usually just create a directory on my home folder, and usually call it dev.

Change your terminal directory to the dev directory

Find where your python 3.4 installed, in case python has been installed elsewhere on your box. Usually it is installed at /usr/bin/python3.4

Create django project virtual environment

Change your directory , and now create a directory and project structure for our django project, and we’ll use python3.4 for the project. change directory to you new
in this instance we have created a project name of demo

We now need to activate the project, i.e tell python we are going to be working in this directory

We can now install django, into our virtual environment

This will install the latest django framework, if you want to install a specific version of the django all you need to do is use the == and the version number required i.e.

This will install the django to your virtual environment.
Start your django project
This command will invoke the django-admin.py script which will set up a new Django project with what_ever_your_project_name_project is for you. A kind of a django developer convention is to add _project, in order to signify the directory where the code files are kept, but there is no hard and fast rule and this is entirely up to you.




You can now try using manage.py script, to run your application issuing the following command
This will instruct django to initiate its lightweight development server. You should see the output in your terminal window
If open the link in your web browser http://127.0.0.1:8000/. You should see a page similar to;
django-itworked

At the time of writing this post the django documentation did not provide sufficient information about PIP and VirtualEnv and I thought I would provide this information for anyone who may have been struggling with this.

Go to Django Project Documentation

Gary Woodfine

Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines. A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.

  • David

    Thanks for this – I did have a slight problem when trying to run “pip install django” – django wasn’t recognised. Error message: “python: can’t open file ‘manage.py’: [Errno 2] No such file or directory”.
    Fixed this by moving to the demo directory and running “pip install django” without “sudo” – ref: http://stackoverflow.com/questions/19326589/running-django-in-virtualenv-on-ec2-importerror-no-module-named-django-core/25271144#25271144

    • threenine

      thanks for the feedback David. Will take a look and update if needed.

  • Franco

    Thanks, it was very useful, also if I’d prefer don’t use so many sudo. IMHO sudo is necessary only for the apt-get install python-pip. After that, simply:
    cd dev

    # create a virtual env
    which python3.4
    virtualenv -p /usr/bin/python3.4 demo
    source demo/bin/activate
    cd demo
    pip install django

    # create a django project
    django-admin startproject my_first_project
    cd my_first_project/
    python manage.py runserver

    • threenine

      If it works for you. Fill your boots.
      have fun playing with Django!

  • very nice

π