To create a new Django application you must have the following things installed on your computer:
- A virtual environment
Before We begin
Throughout this tutorial I will give you instructions for Windows, Ubuntu/Fedora and Mac. If commands vary then I will give you instructions for Windows, Ubuntu/Fedora and Mac. However if command is same for every OS then I will only show instructions for Windows only.
Installing Python on Windows
In this tutorial we will use Python 3.4. Let's start by installing python on Windows first.
If you are using Windows download the Python setup from https://www.python.org/ftp/python/3.4.4/python-3.4.4.msi.
After downloading the installer double click to open it. Select "Install for all users" and click Next.
In the next window installer will ask you to select destination directory, keep it to default (i.e
C:\Python34\ ) and click next.
This step is the most crucial. In this step the installer will ask you to customize the Python installation. Scroll down a little and select "Add python.exe to PATH", click the dropdown icon in front of "Add python.exe to PATH" and select "Will be installed on local hard drive". This allows you to execute
python.exe in Command Prompt from anywhere in the system without specifying the full path.
Click next. At this point you may get a confirmation window like the following , asking you "Do you really want to install this software on your computer. Select Yes to proceed.
Installer will take 1 or 2 minutes depending upon speed of your system to install python on your computer.
Click finish to exit the installer. Now you have installed Python on your Windows computer.
Python Package Manager
In Python we use
pip (Python Package Index) to install and manage different packages(libraries) available at https://pypi.python.org/pypi. It is important to note that
pip itself is a package and is used to install other packages. The packages you install using
pip will be installed in the directory
C:/python34/lib/site-packages. Windows installer automatically installs
pip by default, so you don't need to do anything else. To check which version of
pip is installed on your system execute the following command.
C:\Users\Q>pip --version pip 7.1.2 from c:\python34\lib\site-packages (python 3.4) C:\Users\Q>
As you can see this system has
pip 7.1 which belongs to Python 3.4.
Installing Python on Linux
On a Linux distribution like Ubuntu or Fedora, it is highly likely that Python 3.4 is installed. To check open terminal and type the following commands.
Q@VM1:~$ python3 --version Python 3.5.1
As you can see my Ubuntu machine has Python 3.5 installed by default. Although the whole tutorial is geared towards Python 3.4, you would be fine if you choose to use Python 3.4 or 3.5. However, If you want to follow along using Python 3.4 in Ubuntu type the following commands in the terminal to instal Python 3.4.
Q@VM1:~$: sudo add-apt-repository ppa:fkrull/deadsnakes Q@VM1:~$: sudo apt-get update Q@VM1:~$: sudo apt-get install python3.4
Execute each of these commands one by one on the command line. These commands first downloads the Python from the the
ppa:fkrull/deadsnakes repository and then installs it on your system. The whole process may take some time so please be patient. To invoke Python 3.4 type
python3.4 instead of
python3 in the command line as follows:
Q@VM1:~$ python3.4 Python 3.4.5 (default, Jul 15 2016, 16:39:07) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
To exit the python shell hit Ctrl+D or type
quit() in the shell.
Python installer for Ubuntu doesn't install pip. To install it execute the following command:
Q@VM1:~$ sudo apt-get install python3-pip
To verify the
pip installation execute the following command.
Q@VM1:~$ pip3 --version pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5) Q@VM1:~$
pip3 is installed as a package which belongs to Python 3.5 not Python 3.4.
On Fedora you can install python 3.4 using the following command.
$: sudo yum install python3
Installing Python on Mac
To install Python 3.4 on Mac go to https://www.python.org/downloads/release/python-344/ and scroll down to bottom under Files select any installer corresponding to Mac OS X.
If you are using 32-bit system download Mac OS X 32-bit i386/PPC installer and if you are using 64-bit download Mac OS X 64-bit/32-bit installer. On a 64-bit system both installer will work.
Mac installer also comes with
pip so you don't need to do anything else.
Installing Python Virtual Environment
Create a new directory named TGDB (short for The Great Django Blog). We will use this folder to store our Django application. You can create this directory anywhere, location doesn't really matter. I am using Windows and I have created this directory in
Q is my username.
So what is Virtual Environment ?
A virtual environment helps us to run isolated instance of python/django projects on your machine without conflicting with one another. To understand the philosophy behind Virtual Environment consider the following example:
Let's say we are working on two projects, a blog and a forum for two different clients. Our blog uses a library called
super_library_v02, on the other hand our forum uses
super_library_v01. At a given point in time we can only have a single version of
super_library installed on our system. we can't have both version simultaneously. A Virtual Environment helps us to tackle these kinds of problems easily.
A Virtual Environment solves this problem by creating a virtual environment along with a separate Python installation. So no matter what libraries you install in this virtual environment using
pip, will not conflict with the libraries available at the system wide Python installation.
The package required to create these isolated environments are called
virtualenv on Windows open command prompt and type the following command.
C:\Users\Q\TGDB>pip install virtualenv
To install virtualenv in Ubuntu/Fedora type the following commmand.
Q@VM1:~/TGDB$ pip3 install virtualenv
To create virtual environment on windows type the following command.
virtualenv uses version of the Python which was used to installed it (using the
pip command). In the case of Windows, it is Python 3.4 whereas in Ubuntu it is Python 3.5.
C:\Users\Q>pip --version pip 7.1.2 from c:\python34\lib\site-packages (python 3.4)
To specify any other version of the Python use
-p parameter as follows:
C:\Users\Q\TGDB>virtualenv env -p C:/Python27/python.exe
To create virtual environment in Ubuntu use the following command.
Q@VM1:~/TGDB$ virtualenv env Using base prefix '/usr' New python executable in /home/tom/my_workspace/env/bin/python3 Also creating executable in /home/tom/my_workspace/env/bin/python Installing setuptools, pip, wheel...done. Q@VM1:~/my_workspace$
The above command would create a virtual environment using Python 3.5 because
virtualenv package was installed using
pip3 package of Python 3.5.
In the case you want to follow along using Python 3.4 in Ubuntu, first delete the
env directory created by
virtualenv command and then create a new virtual environment using Python 3.4 using the following command.
Q@VM1:~/TGDB$ virtualenv env -p /usr/bin/python3.4 Running virtualenv with interpreter /usr/bin/python3.4 Using base prefix '/usr' New python executable in /home/tom/my_workspace/env/bin/python3.4 Also creating executable in /home/tom/my_workspace/env/bin/python Installing setuptools, pip, wheel...done. Q@VM1:~/my_workspace$
To know the absolute path of Python 3.4 use the
Q@VM1:~/TGDB$ which python3.4 /usr/bin/python3.4 Q@VM1:~/TGDB$
virtualenv command creates a isolated environment, a directory named env for developing applications using Django/Python. Once
virtualenv finished setting up new virtual environment, open Windows Explorer or Nautilus in Ubuntu to view the files and folders
virtualenv has created for you inside
env directory. As you can can see the
virtualenv command has created four directories and one file inside the
So what these files and folder contains ?
These files and folder constitutes a separate python installation. Any libraries or packages you install here will be available only inside this virtual envionment, so you can work on your project without conflicting with other packages installed on the system.
We have created a virtual environment in the last step, to use it we first have to activate it. To activate virtual environment type the following command.
C:\Users\Q\TGDB>env\Scripts\activate.bat (env) C:\Users\Q\TGDB>
(env) in front of path name indicates that your virtual environment is up and running. To deactivate to virtual environment issue the following command.
Q@VM1:~/TGDB$ source env/bin/activate (env) q@VM1:~/TGDB$
To deactivate simply type deactivate.
(env) tom@VM1:~/my_workspace$ deactivate Q@VM1:~/my_workspace$
In the next step we will install Django.
In this tutorial we will use Django 1.10. To install Django open terminal and type the following command.
(env) C:\Users\Q\TGDB>pip install django==1.10.5
This command fetches the Django framework and installs it into your virtual environment. The output of the command should look like this:
>pip install django==1.10 Collecting django==1.10 Downloading Django-1.10-py2.py3-none-any.whl (6.8MB) 100% |################################| 6.8MB 80kB/s Installing collected packages: django Successfully installed django-1.10 (env) C:\Users\Q\TGDB
If you just want to install current stable version simply issue the following command.
>pip install django
Testing the installation
Inside the virtual environment start the Python shell by typing
>python Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AM D64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
To verify whether installation was successfull or not, import django module and call
get_version() method on it.
>>> import django >>> django.get_version() '1.10' >>>
If everything went fine you should get the version of the django installed. If you have encountered any error go through all the above steps or post a comment in the box below and we will figure out the problem.
To exit the python shell type Ctrl + Z (in Windows), Ctrl + D (in ubuntu/Fedora/Mac) or just type
>>> import django >>> django.get_version() '1.10.5' >>> ^Z (env) C:\Users\Q\TGDB