-
Hajipur, Bihar, 844101
A virtual environment is one of the most important tools you will use as a Python developer. It creates a separate workspace for each project so that the packages you install for one project do not affect any other project on your system. Most real-world Python applications depend on many external libraries, and each project may require its own set of versions. A virtual environment helps you manage these dependencies safely and neatly.
In this tutorial, you will learn what a virtual environment is, why developers rely on it, how to create one, how to activate and deactivate it, how to install packages inside it and how to keep your project easier to share with others using a requirements file. By the end, you will be confident creating and using virtual environments in all your Python projects.
A virtual environment is a folder that contains its own Python interpreter and a private location for packages. This folder works like a bubble where only the libraries installed inside it are available. If you install Django in one environment and Flask in another, neither project will interfere with the other.
For example:
A data science project may need numpy 1.26
A web app may need numpy 1.23
One project may require pandas
Another may not need it at all
If all packages were installed globally, these versions would clash. Virtual environments solve this by keeping everything project-specific.
Here are the main reasons developers use virtual environments:
Each project gets its own set of dependencies
No conflicts between package versions
Your main Python installation stays clean
You can work on multiple projects at the same time
It becomes easier to share or deploy your project
Many frameworks expect you to work inside an environment
Once you start using virtual environments, it becomes a natural part of your workflow because it removes a lot of the frustration caused by dependency issues.
Before creating a virtual environment, check your Python version so you know which interpreter will be used.
python --version
or
python3 --version
Use the command that works on your operating system.
Python includes a built-in module called venv that you can use to create virtual environments.
python -m venv myenv
Here:
python -m venv is the command
myenv is the name of the virtual environment
You can choose any name:
env
project_env
test_env
django_env
Once the command runs, a folder with the chosen name will appear. This folder contains everything needed for that environment.
You must activate the environment to start using it.
myenv\Scripts\activate
source myenv/bin/activate
When the environment is active, your terminal prompt will show the environment name:
(myenv)
This tells you that any package you install will go inside this environment only.
After activating the environment, you can use pip normally. The only difference is that packages install inside the environment instead of the system.
Example:
pip install requests
Another:
pip install django
These packages will appear in the myenv folder. Other projects will not see them, which keeps everything clean and isolated.
To view all packages inside the environment, use:
pip list
This is helpful when you want to confirm which versions you’re working with.
When you finish working, you can deactivate the environment by running:
deactivate
Your terminal prompt returns to normal, and Python switches back to the global installation.
If you no longer need an environment, you can safely delete the entire folder:
myenv
Since all installed packages live inside that folder, deleting it removes the entire environment.
Virtual environments are common in real-world development. Almost every Django, Flask, FastAPI or data-science project uses one.
python -m venv django_env
source django_env/bin/activate
pip install django
django-admin startproject myproject
This ensures your Django project uses the correct library versions without affecting other projects.
python -m venv flask_env
source flask_env/bin/activate
pip install flask
python app.py
This keeps your project independent from the rest of your system.
Developers often keep track of project dependencies using a requirements.txt file. This file lists all packages required by the project along with their versions.
pip freeze > requirements.txt
This command scans all installed packages inside the environment and writes them to the file.
If you want to recreate the same environment on another machine:
pip install -r requirements.txt
This makes collaboration easier and keeps your project consistent across teams and servers.
Create an environment
python -m venv env
Activate on Windows
env\Scripts\activate
Activate on Linux or Mac
source env/bin/activate
Install a package
pip install numpy
Install multiple packages
pip install flask requests pandas
Upgrade a package
pip install --upgrade requests
List installed packages
pip list
Save packages
pip freeze > requirements.txt
Install from a requirements file
pip install -r requirements.txt
Deactivate environment
deactivate
A virtual environment helps you isolate dependencies for each Python project so your system stays clean and organized. You learned how to create, activate and deactivate environments, install packages inside them and use requirement files to share or rebuild your setup. Virtual environments prevent version conflicts and make your projects easier to manage, which is why they are used in almost every serious Python application.
Q1. Write a Python command to create a virtual environment called projectenv.
Q2. Write a command to activate the virtual environment on Windows.
Q3. Write a command to install the django package inside the virtual environment.
Q4. Write a command to list all installed packages inside the virtual environment.
Q5. Write a command to deactivate the virtual environment.
Q6. Write a command to create a requirements.txt file using pip freeze.
Q7. Write a command to install dependencies from requirements.txt inside a virtual environment.
Q8. Write a command to delete a virtual environment folder named projectenv.
Q9. Write a short explanation or command to use a virtual environment to avoid package conflicts.
Q10. Write steps to use different versions of the same package in two separate virtual environments.