In this article, we are going to learn how to install Django on Windows, Mac and Linux. Since Mac and Linux are both derived from the Unix platform, the instructions for installing Django on Mac and Linux are almost identical to each other and we will present them in the same section. Windows, however, is so different from the other two operating systems that we need use one section to present the instructions about how to install Django on Windows.
Python Virtual Environment Setup
Often Python is already installed on your system if you’re using a mainstream OS such as Windows, Mac OS X or Linux. However, if it is not, you can download and install the appropriate Python version for your OS on the official Python website. Assuming you have Python 2.7.x installed on your OS, we will walk you through the steps to create a Django development environment on Windows, Mac OS X, and Linux in the following sections.
Before we dive into the steps to create a Django development environment, we’d like to review a useful tool called virtualenv
. virtualenv
is a tool to create isolated Python environments. Instead of writing code on top of one global Python environment, virtualenv
allows you to create isolated “islands” or directories of Python environments each of which is an independent Python environment that has its own “system-level” packages and libraries.
- Python’s SQLAlchemy vs Other ORMs
- Build Games for Python Using Pygame | What is PyGame? | Basic PyGame Program
- Python Programming – Import Model
Why do we need virtualenv
since we can just write code running on top of the global Python environment?
Well, let’s imagine a situation where my_library
depends on another package dependent_library
whose version has to be 1.0.0. When you upgrade the global Python environment from 2.7.3 to 2.3.3, dependent_library
also gets upgraded to 1.2.0. Now my_library
won’t work anymore since it’s calling methods and using classes from the 1.0.0 of dependent_library
. Wouldn’t it be nice if you can write my_library
against an independent 1.0.0 dependent_library
as well as another upgraded_my_library
against 1.2.0?
Or imagine that you are programming on a shared hosting environment where your user does not have access to root-level directories such as /usr/lib
which means you cannot modify the global Python environment into a version that you like. Wouldn’t it be nice if you can create a Python environment inside your home directory?
Luckily, virtualenv
solves all the aforementioned issues by creating an environment that has its own installation directories who do not share any library with other environments.
Setup virtualenv and Django in Windows
First, open your browser and navigate to virtualenv. Click the download button to get the source code of the latest virtualenv.
Second, open a Powershell instance and navigate to the directory into which you have downloaded the virtualenv
source code and extract the tar file into a directory. Then you can change into that directory to install virtualenv
for your current Python interpreter which could be invoked from command line.
...> $env:Path = $env:Path + ";C:\Python27" ...> cd virtualenv-x.xx.x ...> python.exe .\setup.py install Note: without Setuptools installed you will have to use "python -m virtualenv ENV" running install running build ......
Now you can create a virtualenv
instance in your home directory.
...> python.exe -m virtualenv python2-workspace New python executable in ... Installing Setuptools... Installing Pip...
Now we can activate the new environment using the activate
script. Note that Windows’ execution policy is restricted by default which means scripts such as activate
cannot be executed. Therefore, we need to change the execution policy to AllSigned in order to be able to activate the virtualenv.
...> Set-ExecutionPolicy AllSigned Execution Policy Change The execution policy ... ... : Y ...> cd python2-workspace ...> .\Scripts\activate (python-workspace) ...>
Notice that once the virtualenv is activated, you will see a string “(python2-workspace)” prepended to your command line’s shell prompt. Now you can install Django inside the new virtual environment.
...> pip install django Downloading/unpacking django ......
Setup virtualenv and Django in Mac OS X and Linux
Installing virtualenv and Django on Mac OS X and Linux is similar to Windows. First, you download the virtualenv
source code, unpack it and install it using your global Python interpreter.
$ curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.10.1.tar.gz % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 1294k 100 1294k 0 0 498k 0 0:00:02 0:00:02 --:--:-- 508k $ tar xvf virtualenv-1.10.1.tar.gz $ cd virtualenv-1.10.1/ $ sudo python setup.py install Password: running install running bdist_egg running egg_info writing virtualenv.egg-info/PKG-INFO writing top-level names to virtualenv.egg-info/top_level.txt writing dependency_links to virtualenv.egg-info/dependency_links.txt writing entry points to virtualenv.egg-info/entry_points.txt reading manifest file 'virtualenv.egg-info/SOURCES.txt' ... Installed /Library/Python/2.7/site-packages/virtualenv-1.10.1-py2.7.egg Processing dependencies for virtualenv==1.10.1 Finished processing dependencies for virtualenv==1.10.1
Then you go back to your home directory and create a new virtualenv in that directory.
$ virtualenv python2-workspace New python executable in python2-workspace/bin/python Installing Setuptools..............................................................................................................................................................................................................................done. Installing Pip.....................................................................................................................................................................................................................................................................................................................................done.
Once the environment has been created, you can activate the environment and install Django in it.
$ cd python2-workspace/ $ pip install django Downloading/unpacking django Downloading Django-1.5.4.tar.gz (8.1MB): 8.1MB downloaded Running setup.py egg_info for package django warning: no previously-included files matching '__pycache__' found under directory '*' warning: no previously-included files matching '*.py[co]' found under directory '*' Installing collected packages: django Running setup.py install for django changing mode of build/scripts-2.7/django-admin.py from 644 to 755 warning: no previously-included files matching '__pycache__' found under directory '*' warning: no previously-included files matching '*.py[co]' found under directory '*' changing mode of /private/tmp/python2-workspace/bin/django-admin.py to 755 Successfully installed django Cleaning up...
Summary and Tips
In this article, we learned how to install virtualenv
in Windows, Mac OS X and Linux and using its pip
command to install Django. Since the virtual environments are separated from the rest of the system, the installed Django library only affects files executed within that particular environment. Compared to Mac OS X and Linux, setting up virtualenv
in Windows requires one extra step to change the execution policy of scripts. Otherwise the steps to set up a Django virtual environment is almost identical across all platforms.