Python VirtualEnv


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.

What Is a Virtual Environment?

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.

Why Do We Use VirtualEnv?

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.

Checking Your Python Version

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.

Creating a Virtual Environment

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.

Activating the Virtual Environment

You must activate the environment to start using it.

On Windows

myenv\Scripts\activate

On macOS and Linux

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.

Installing Packages in a Virtual Environment

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.

Checking Installed Packages

To view all packages inside the environment, use:

pip list

This is helpful when you want to confirm which versions you’re working with.

Deactivating the Virtual Environment

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.

Removing a Virtual Environment

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.

Using VirtualEnv in Real Projects

Virtual environments are common in real-world development. Almost every Django, Flask, FastAPI or data-science project uses one.

Example: Django Project Setup

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.

Example: Flask Application Setup

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.

Working With Requirements Files

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.

Creating a requirements.txt file

pip freeze > requirements.txt

This command scans all installed packages inside the environment and writes them to the file.

Installing from requirements.txt

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.

Practical Examples

  1. Create an environment

python -m venv env
  1. Activate on Windows

env\Scripts\activate
  1. Activate on Linux or Mac

source env/bin/activate
  1. Install a package

pip install numpy
  1. Install multiple packages

pip install flask requests pandas
  1. Upgrade a package

pip install --upgrade requests
  1. List installed packages

pip list
  1. Save packages

pip freeze > requirements.txt
  1. Install from a requirements file

pip install -r requirements.txt
  1. Deactivate environment

deactivate

Summary of the Tutorial

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.


Practice Questions

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.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top