Django Tutorial Part 2: Installing Django in a Virtual Environment
Disclaimer: Your support helps keep JovialGuide running! Our content is reader-supported. This means if you click on some of our links, we may earn a commission.
In this complete beginners guide to Django 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 complete beginners guide to Django, so if you haven’t read part 1, 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 installing 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 is a box or an isolated 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 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 complete beginners guide to Django.
Working with a virtual environment requires you to:
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 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. This folder will house our Django projects. 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, you can 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.
Installing a Virtual Environment in Python
To install a virtual environment in Python, type the following command in your terminal/cmd and allow it to run:
pip install virtualenv
Just like npm (Node Package Manager) for NodeJS, pip is the official package manager for Python.
Creating a Virtual Environment
This command installs a virtual environment in Python. 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/dependencies we will use. To create a virtual environment, copy and paste the following command in your terminal. venv is the name of your virtual environment:
If you have successfully run the command, then you have created a virtual environment. Now we have to activate it!
Activating a Virtual Environment
To activate a virtual environment in Windows, copy and paste the following command in your terminal:
- venv is the name of the virtual environment you created
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) to activate it.
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 version, 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.
To activate a virtual environment, run:
To deactivate a virtual environment, run:
In the next complete beginners guide to Django, we will show you:
We hope this complete beginners guide to Django – installing Django in a virtual environment (part 2), helped you learn how to install Python, and how to install Django in a virtual environment. This is a complete beginners guide to Django, so if you haven’t read part 1, then see Part 1: introduction to Django to get started.
See other of our Django tutorials for more.