In this beginner Django tutorial series part 2, we will show you how to install Python, install, create and activate a virtual environment in Python and how to install Django in a virtual environment. This is a Django tutorial series, so if you haven’t read the first part then see Part 1: introduction to Django to get started.
Downloading and Installing Python
Since Django is built with Python, we need to install Python first before we can get started with Django. This is like the first step to using Django!
If you do not already have Python installed, goto Python downloads, select your operating system, system type and download Python.
After you have successfully downloaded Python, right click and run as administrator.
You will be welcomed with the Python installer screen. Now tick the Add Python to Path option and click on the Install Now button and allow the installer to complete.
If you have installed Python successfully, then you will see a screen like the screenshot below, if not then restart the installation process.
Now you have successfully downloaded and installed Python, let us look at Python virtual environment.
Installing a Virtual Environment in Python
For us to understand what a Python virtual environment is, we will start by defining it.
A virtual environment in Python is a box or an isolated Python environment where the dependencies of a project are installed in a separate or different directory from those installed in the system’s default path or environment.
Now, you may ask, what problem does virtual environments solve? Virtual environment solves the following problems:
- Isolating your Python project from other Python projects on your computer
- You can use any version of Python or Python package without having to worry about version clash
- Your Python package directory is organized, so you are not bombed with unwanted packages
Note – A Python virtual environment is not mandatory, but it is highly recommended in this Django tutorial series.
Working with a virtual environment requires you to:
- Install a virtual environment
- Create a virtual environment and
- Activate the virtual environment
We will need the terminal that is built into VS Code or the command prompt that comes with Windows. If you have any other terminal aside the command prompt, then it is fine to use it!
If you have not already downloaded VS Code, then goto Download VS Code to get started.
After VS Code has been downloaded and installed, create a folder on your desktop and name it Django. You can give it any other name if you want. After that, right click and open with VS Code.
This is the time we need the terminal!
To open the VS Code terminal, click Terminal » New Terminal.
This is what VS Code terminal looks like:
Alternatively, press ctrl + shift + ` on VS Code.
To open command prompt on Windows, search for cmd.
This is what command prompt looks like:
If you are using the Windows command prompt, we have to get into the Django directory we created on the Desktop. Type cd desktop to get into the Desktop of your computer. Again type cd django to change directory to the Django directory you created on the Desktop.
To install a virtual environment in Python, type the following command in your terminal and allow it to run.
pip install virtualenv
Just like npm (Node Package Manager) for NodeJS, pip is the official package manager for Python.
This command installs a virtual environment in Python for you. Now that we have installed a virtual environment in Python, the next step is to actually create a virtual environment that will house our Django project and the packages we will use. To create a virtual environment, copy and paste the following command in your terminal. django_series is the name your virtual environment.
If you have successfully run the command, then you have created a virtual environment. Now we have to activate it!
To activate a virtual environment in Windows, copy and paste the following command in your terminal.
If you see the name of the virtual environment you created in a bracket, then virtual environment has been activated. Something like this:
Installing Django in a Virtual Environment
We have installed Python, installed, created and activated a virtual environment in Python. The next step is to install Django in the virtual environment.
Since Django is a Python framework, it is available for installation via pip. To install Django, make sure your virtual environment is activated. If not, then follow the section above (Installing a virtual environment in Python)
If your virtual environment has been activated, copy and paste the following command into your terminal and allow it to finish.
pip install django
This command will install Django into the virtual environment (latest version of Django) you activated.
After you have installed Django, let’s verify that Django has been installed successfully using the version checker command. Copy and paste the following command into your terminal.
python -m django --version
If you see a number, then you have installed Django successfully. This number is the version of Django in your computer. However, if you get an error, then you did not install Django successfully.
We hope this Django tutorial series – installing Django (Part 2) helped you learn how to install Django, virtual environment and Python. This is a Django tutorial series, so if you haven’t read the first part then see Part 1: introduction to Django to get started.
See other of our Django tutorials for more.