To create a new Django application you must have the following things installed on your computer:
- A Virtual Environment.
Installing Python on Windows
In this tutorial, we will use Python 3.7.4. Let’s start by installing Python on Windows first.
Note: Throughout this course instructions are given for Windows, Ubuntu, and Mac. Most of the commands will work no matter which OS you are using. However, there are some commands which vary from one system to another. If that’s the case, I have clearly mentioned it and provided commands specific to the system.
Windows users can download the Python setup from https://www.python.org/downloads/release/python-374/
After downloading the installer double click to open it. Select “Install for all users” and click Next.
Python Package Manager
In Python, we use
pip (Python Package Index) to install and manage different packages (or 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:/python37/lib/site-packages. Windows installer automatically installs
pip by default, so you don’t need to do anything else. To check the version of
pip installed on your system execute the following command.
C:\Users\kwikl3arn>pip --version pip 19.0.3 from C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1264.0_x64__qbz5n2kfra8p0\lib\site-packages\pip (python 3.7) C:\Users\kwikl3arn>
As you can see, this system has
pip 19.0.3 and is installed under Python 3.7.
Installing Python on Linux
On a Linux distribution like Ubuntu or Fedora, it is highly likely that Python 3.4 or above is installed. To check, open terminal and type the following command.
$ 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, it would be perfectly fine if you choose to use Python 3.5. We will be using Django 1.10 in this course which only works with Python 3.4 and 3.5. So make sure you have either Python 3.4 or 3.5 installed on your system. Trying to use Django 1.10 with some other version of Python may result in unexpected issues.
If you want to work with Python 3.4 instead of Python 3.5 in Ubuntu type the following commands in the terminal.
[email protected]:~$: sudo add-apt-repository ppa:fkrull/deadsnakes [email protected]:~$: sudo apt-get update [email protected]:~$: sudo apt-get install python3.4
Execute each of these commands one by one on the command line. These commands first download the Python from
ppa:fkrull/deadsnakes repository and then installs it on your system. On a slow connection 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:
$ python3.4 Python 3.4.5 (default, Nov 15 2019, 16:39:07) [GCC 5.4.0 20190609] 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:
$ sudo apt-get install python3-pip
To verify the
pip installation execute the following command.
[email protected]:~$ pip3 --version pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5) [email protected]:~$
Installing Python Virtual Environment
Create a new directory named
TGDB (short for “The Great Django Blog”) using the
C:\Users\kwikl3arn>mkdir TGDB C:\Users\kwikl3arn>
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
C:\Users\kwikl3arn where kwikl3arn is my username. Once done, change your current working directory to
TGDB using the
cd command, as follows:
C:\Users\kwikl3arn>cd TGDB C:\Users\kwikl3arn\TGDB>
Now we are ready to install Virtual Environment.
So what is this Virtual Environment?
A virtual environment helps us to run isolated instances of Python/Django projects on a 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 versions simultaneously. A Virtual Environment helps us to tackle these kinds of problems easily.
A Virtual Environment solves this problem by creating a separate Python installation. So, no matter what libraries you install on a particular 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 is called
virtualenv on Windows open command prompt and type the following command.
C:\Users\kwikl3arn\TGDB>pip install virtualenv
virtualenv in Ubuntu/Mac type the following command.
[email protected]:~/TGDB$ pip3 install virtualenv
To create virtual environment type
virtualenv command followed by the name of the virtual environment. Here is how you can create a virtual environment in Windows:
virtualenv creates virtual environment using the version of Python under which it is installed. In the case of Windows, it is Python 3.4 whereas in Ubuntu it is Python 3.5.
C:\Users\kwikl3arn>pip --version pip 19.0.3 from C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1264.0_x64__qbz5n2kfra8p0\lib\site-packages\pip (python 3.7)
To specify any other version of the Python use
-p option as follows:
C:\Users\kwikl3arn\TGDB>virtualenv env -p C:/Python27/python.exe
The above command will create a virtual environment using Python 2.7. Before specifying the version of the python using the
-p option make sure that the version of the Python specified is installed on your computer.
We can use the same command as Windows to create a virtual environment in Ubuntu/Mac:
[email protected]:~/TGDB$ virtualenv env
In Ubuntu, the above command will create a virtual environment using Python 3.5 because
virtualenv was installed as a package of Python 3.5.
In case you want to follow along using Python 3.4 in Ubuntu, delete the
env directory created by
virtualenv command and then create a new virtual environment using Python 3.4, as follows:
[email protected]:~/TGDB$ virtualenv env -p /usr/bin/python3.4
To know the absolute path of Python 3.4 use the
[email protected]:~/TGDB$ which python3.4 /usr/bin/python3.4 [email protected]:~/TGDB$
So what actually
virtualenv command does?
virtualenv command creates an isolated environment, a directory which we named
env for developing applications using Django/Python. Once
virtualenv finished setting up a new virtual environment, open Windows Explorer or Nautilus in Ubuntu to view the files and folders
virtualenv has created for us inside the
In Windows, the contents of
env directory should look like this:
In Ubuntu/Mac, the contents of
env directory should look like this:
So what these files and folder contain?
These files and folder constitute a separate python installation. Any libraries or packages you install here will be available only inside this virtual environment, 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.
Activating virtualenv in windows
To activate virtual environment in Windows type the following command.
C:\Users\kwikl3arn\TGDB>env\Scripts\activate (env) C:\Users\kwikl3arn\TGDB>
Activating virtualenv in Ubuntu/Mac
On Linux/Mac, we use
source command to activate virtual environment.
[email protected]:~/TGDB$ source env/bin/activate (env) [email protected]:~/TGDB$
(env) in front of the prompt string, it indicates that your virtual environment is up and running. From this point on, any package you add or remove using
pip will only affect this virtual environment. Your system-wide Python installation will remain intact. We can use
pip list command to view packages installed in this virtual environment
(env) C:\Users\kwikl3arn\TGDB>pip list
This virtual environment has 3 packages installed. It is important to note that once the virtual environment is active you can invoke pip either using
pip3. This is true for Window, Ubuntu as well as Mac.
To deactivate virtual environment issue the following command.
This command is same for Windows, Ubuntu, and Mac. Now we are out of the virtual environment. Run
pip list command again, but this time it will show you all the system-wide packages installed on your system.
C:\Users\kwikl3arn\TGDB>pip list certifi (2017.4.17) chardet (3.0.4) colorama (0.3.9) decorator (4.0.11) httpie (0.9.9) idna (2.5) ipython (6.1.0) ipython-genutils (0.2.0) jedi (0.10.2) olefile (0.44) pickleshare (0.7.4) pip (7.1.2) ... C:\Users\kwikl3arn\TGDB>
... indicates that the code snippet is truncated to save space.
On Ubuntu/Mac, you should use
pip3 list to view system-wide packages installed on the system.
[email protected]:~$ pip3 list apturl (0.5.2) beautifulsoup4 (4.4.1) blinker (1.3) Brlapi (0.6.4) chardet (2.3.0) checkbox-support (0.22) command-not-found (0.3) cryptography (1.2.3) defer (1.0.6) feedparser (5.1.3) guacamole (0.9.2) html5lib (0.999) ... httplib2 (0.9.1)
In this tutorial, we will use Django 2.22. To install Django, activate virtual envionment and then type the following command.
(env) C:\Users\kwikl3arn\TGDB>pip install django==2.22
This command fetches the Django framework from PyPI and installs it into your virtual environment. The output of the command should look like this:
(env) C:\Users\kwikl3arn\TGDB>pip install django==2.22 Collecting django==2.22 Downloading Django-2.22-py2.py3-none-any.whl (6.8MB) 100% |################################| 6.8MB 80kB/s Installing collected packages: django Successfully installed django-2.22 (env) C:\Users\kwikl3arn\TGDB>
If you just want to install current stable version simply issue the following command.
(env) C:\Users\kwikl3arn\TGDB>pip install django