Installing Django
If you have already installed Django, you can skip this section and jump directly to the Creating your first project section. Django comes as a Python package and thus can be installed in any Python environment. If you haven't installed Django yet, the following is a quick guide to install Django for local development.
Django 2.0 requires Python version 3.4 or higher. In the examples for this book, we will use Python 3.6.5. If you're using Linux or macOS X, you probably have Python installed. If you are using Windows, you can download a Python installer at https://www.python.org/downloads/windows/.
If you are not sure whether Python is installed on your computer, you can verify it by typing python
in the shell. If you see something like the following, then Python is installed on your computer:
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
If your installed Python version is lower than 3.4, or if Python is not installed on your computer, download Python 3.6.5 from https://www.python.org/downloads/ and install it.
Since you will use Python 3, you don't have to install a database. This Python version comes with a built-in SQLite database. SQLite is a lightweight database that you can use with Django for development. If you plan to deploy your application in a production environment, you should use an advanced database, such as PostgreSQL, MySQL, or Oracle. You can get more information about how to get your database running with Django at https://docs.djangoproject.com/en/2.0/topics/install/#database-installation.
Creating an isolated Python environment
It is recommended that you use virtualenv
to create isolated Python environments, so that you can use different package versions for different projects, which is far more practical than installing Python packages system-wide. Another advantage of using virtualenv
is that you won't need any administration privileges to install Python packages. Run the following command in your shell to install virtualenv
:
pip install virtualenv
After you install virtualenv
, create an isolated environment with the following command:
virtualenv my_env
This will create a my_env/
directory, including your Python environment. Any Python libraries you install while your virtual environment is active will go into the my_env/lib/python3.6/site-packages
directory.
Note
If your system comes with Python 2.X and you have installed Python 3.X, you have to tell virtualenv
to use the latter.
You can locate the path where Python 3 is installed and use it to create the virtual environment with the following commands:
zenx$ which python3 /Library/Frameworks/Python.framework/Versions/3.6/bin/python3 zenx$ virtualenv my_env -p /Library/Frameworks/Python.framework/Versions/3.6/bin/python3
Run the following command to activate your virtual environment:
source my_env/bin/activate
The shell prompt will include the name of the active virtual environment enclosed in parentheses, as follows:
(my_env)laptop:~ zenx$
You can deactivate your environment at any time with the deactivate
command.
You can find more information about virtualenv
at https://virtualenv.pypa.io/en/latest/.
On top of virtualenv
, you can use virtualenvwrapper
. This tool provides wrappers that make it easier to create and manage your virtual environments. You can download it from https://virtualenvwrapper.readthedocs.io/en/latest/.
Installing Django with pip
The pip
package management system is the preferred method for installing Django. Python 3.6 comes with pip
preinstalled, but you can find pip
installation instructions at https://pip.pypa.io/en/stable/installing/.
Run the following command at the shell prompt to install Django with pip
:
pip install Django==2.0.5
Django will be installed in the Python site-packages/
directory of your virtual environment.
Now, check whether Django has been successfully installed. Run python
on a terminal, import Django, and check its version, as follows:
>>> import django >>> django.get_version() '2.0.5'
If you get the preceding output, Django has been successfully installed on your machine.
Note
Django can be installed in several other ways. You can find a complete installation guide at https://docs.djangoproject.com/en/2.0/topics/install/.