How to Create a Virtual Environment in Python for Django Application?
We are going to cover everything you need to know about virtual environments and how to create a new environment and set one up with Virtualenv. We will use virtual environments for Django applications.
What is a Virtual Environment?
A virtual environment is a self-contained environment where you can install and manage Python packages separately from your system's Python installation. It allows you to isolate your Django app's dependencies, including the Python interpreter, libraries, and scripts, from other projects and the system-wide Python setup. This isolation helps prevent conflicts between different packages and ensures that your Django app has its own clean and consistent environment to run in.
we will explore how to install packages using pip and manage your Python environment using either venv (for Python 3) or virtualenv (for Python 2). These tools provide a foundational level of control for managing Python packages. They are recommended when other higher-level tools are not suitable for your requirements.
1. Install pip
pip is a tool used for managing Python packages. It allows you to install and update packages for your Python projects. To ensure you have the latest version of pip installed, you need to check and update it if necessary.
You can install pip yourself to ensure you are running the latest version. Check if pip is already installed on your system by running the command pip --version in your terminal or command prompt.
python3 -m pip install --user --upgrade pip
python3 -m pip --version
Output will be something like this:
pip 23.1.2 from C:\Users\kalyan\AppData\Local\Programs\Python\Python311\Lib\site-packages\pip (python 3.11)
2. Install virtualenv
If you are using Python version 3.3 or newer, you can use the venv module to create and manage virtual environments. The venv module provides a recommended way to create isolated environments for any Python projects you create. It allows you to keep your project dependencies separate from other Python installations on your system, providing better control and organization.
By using the venv module, you can create a virtual environment specifically tailored to your project, install packages and libraries within that environment, and making it sure that your project are running on its own isolated Python environment. This helps in maintaining project dependencies, avoiding conflicts between different projects, and ensuring consistent behavior across different development environments.
In simple terms, the venv module allows you to create a sandboxed environment for your Python projects, where you can install and manage project-specific dependencies without affecting other Python installations on your system.
You can install virtualenv using pip.
For Unix (Linux) / Mac OS:
python3 -m pip install --user virtualenv
For Windows:
py -m venv env
venv will create a virtual Python installation in the env folder.
To avoid including your virtual environment directory (for example: env named folder) in your version control system (such as Git), you should exclude it using a file called .gitignore. This file specifies which files and directories should be ignored by Git.
In simple terms, by excluding the virtual environment directory from version control, you prevent it from being stored in your repository, ensuring that each developer can create their own virtual environment and install the required packages independently. This helps maintain a clean and portable project structure.
3. Activate a virtual environment
You’ll need to activate the virtual environment before installing or using packages. Activating a virtual environment will put the virtual environment-specific python and pip executables into your shell’s PATH. It means any packages or libraries you install within the virtual environment are isolated and do not interfere with other Python installations on your system.
For Unix / Mac OS:
source env/bin/activate
For Windows:
.\env\Scripts\activate
Checking the location of your Python interpreter you can confirm that you’re in the virtual environment:
For Unix / Mac OS: which python
For Windows: where python
It should be in the env directory:
For Unix / Mac OS:
.../env/bin/python
For Windows:
...\env\Scripts\python.exe
When your virtual environment is activated, pip will install packages into that environment, allowing you to use and import those packages in your Python application.
How to leave or deactivate the virtual environment?
If you want to switch projects or otherwise leave your virtual environment, you can simply run this in you terminal:
deactivate
To re-enter the virtual environment, simply activate it using the same instructions as before. No need to recreate the virtual environment.
I have created this guide from the official source given below:
Read more about installing packages and libraries using pip.
Installing packages using pip and virtual environments
Now you have a clean and isolated environment to work on your Django project. You can install Django and any other dependencies specific to your project without affecting your system-wide Python installation.