In this lesson, you will learn about Python virtual environments and how they are used in Django projects. A virtual environment creates an isolated Python environment for a project.
A Python virtual environment is an isolated environment where Python packages can be installed separately from the system Python installation.
It is commonly used when developing Django and other Python applications.
Different projects may require different versions of Python packages. A virtual environment keeps project dependencies separated.
Django is installed as a Python package. Installing Django inside a virtual environment means that the Django installation belongs to that particular project environment.
python -m pip install Django
Python provides the venv module for creating virtual environments.
python -m venv venv
The first venv refers to the Python module, while the second
venv is the name of the environment folder.
The environment folder can have another name. For example:
python -m venv myenv
This creates a virtual environment named myenv.
On Windows, you can create an environment using:
python -m venv venv
A directory named venv is created inside the current project
directory.
Linux and macOS users can create an environment using:
python3 -m venv venv
This creates the virtual environment in the venv directory.
After creating the environment, activate it on Windows with:
venv\Scripts\activate
After activation, the environment name normally appears in the terminal.
On Linux and macOS, activate the environment using:
source venv/bin/activate
When the environment is activated, its name generally appears before the command prompt.
(venv) C:\DjangoProject>
Here, venv indicates that the virtual environment is active.
After activating the environment, install Django using pip:
python -m pip install Django
Django will be installed into the active environment.
After installing Django, verify the installation:
python -m django --version
The command displays the Django version installed in the active environment.
Packages installed inside a virtual environment are kept separate from packages installed globally.
This allows different projects to use different package versions.
The system Python installation is available outside the virtual environment. Packages installed globally can potentially be used by multiple projects.
For project development, an isolated environment is usually easier to manage.
When you create a virtual environment named venv, Python
creates a directory containing the environment's files.
python -m venv venv
The folder should generally be treated as an environment directory rather than as application source code.
When you finish working with the environment, you can deactivate it using:
deactivate
After deactivation, the terminal returns to the normal system Python environment.
A virtual environment can be activated again whenever you return to the project.
Windows:
venv\Scripts\activate
Linux/macOS:
source venv/bin/activate
After activating the environment, you can see the installed packages with:
python -m pip list
This is useful for checking which packages are installed in the active environment.
You can check which Python executable is being used.
On Windows:
where python
On Linux/macOS:
which python
This can help confirm whether the active environment's Python is being used.
You can create separate virtual environments for different Django projects.
For example:
ProjectA/
venv/
ProjectB/
venv/
Each project can manage its dependencies independently.
Suppose one project needs one version of a package while another project needs a different version. Installing everything globally can create dependency conflicts.
Virtual environments help keep these project dependencies separated.
Python projects commonly use a file named requirements.txt
to record required packages.
A package list can be generated with:
python -m pip freeze > requirements.txt
This records installed package versions in the current environment.
Another environment can install the packages listed in
requirements.txt.
python -m pip install -r requirements.txt
This is useful when setting up an existing Python or Django project.
The virtual environment contains installed packages and environment-specific files. It is normally recreated on another computer instead of being copied as part of the project source.
A requirements.txt file can be used to describe the required packages.
A typical workflow is:
Example:
mkdir MyDjangoProject
cd MyDjangoProject
python -m venv venv
venv\Scripts\activate
python -m pip install Django
python -m django --version
Example:
mkdir MyDjangoProject
cd MyDjangoProject
python3 -m venv venv
source venv/bin/activate
python -m pip install Django
python -m django --version
A common mistake is installing Django before activating the intended virtual environment.
If Django is installed in the wrong environment, your project may not find the expected package.
Always activate the correct environment before installing project packages.
For each Django project, create and activate an appropriate virtual environment before installing project dependencies.
This keeps your development environment cleaner and makes dependency management easier.
A virtual environment provides an isolated Python environment for a project. It is especially useful for Django development because projects can have separate dependencies.
python -m venv venv
venv\Scripts\activate
python -m pip install Django
On Linux/macOS, activation is commonly done with:
source venv/bin/activate
venv module for creating environments.python -m venv venv to create an environment.deactivate to leave the active environment.requirements.txt can record project dependencies.Question: Which command creates a Python virtual environment named venv?