Introduction

When building Python projects, managing dependencies correctly is very important. Without proper control, your system can become messy with conflicting package versions, leading to your project breaking. That is why Python developers use virtual environments — isolated spaces where each project can have its own packages, versions, and dependencies. In this article, you will learn in simple words how to create virtual environments, install packages, manage dependencies, and keep your Python projects clean and stable.

What Is a Virtual Environment in Python?

A virtual environment is a separate workspace for each Python project. It lets you install specific libraries without affecting other projects or your system Python.

Why You Need It

Example

Project A needs Django 3
Project B needs Django 5
A virtual environment allows both to exist without conflict.

How to Create a Virtual Environment Using venv

Python comes with a built-in module called venv for creating environments.

Step 1: Create the Virtual Environment

Bash
python3 -m venv myenv

This creates a folder named myenv that contains your isolated Python environment.

Step 2: Activate the Environment

On Windows:

Bash
myenv\Scripts\activate

On macOS / Linux:

Bash
source myenv/bin/activate

Step 3: Deactivate

Bash
deactivate

How You Know It’s Activated

Your terminal will show:

Code
(myenv) C:\Users\User>

Installing Dependencies Inside the Virtual Environment

Once the environment is activated, you can install packages using pip.

Example

Bash
pip install numpy pandas flask

These packages will be installed only inside the virtual environment.

Check Installed Packages

Bash
pip list

Managing Dependencies with requirements.txt

A requirements.txt file keeps track of all packages needed for a project.

Create requirements.txt Automatically

Bash
pip freeze > requirements.txt

This file stores exact versions of installed packages.

Install from requirements.txt

Bash
pip install -r requirements.txt

This ensures every user gets the same package versions.

Why This Is Important

Creating Multiple Virtual Environments for Different Projects

Each project should have its own environment.

Example

Bash
python -m venv projectA_env
python -m venv projectB_env

Switch environments by activating and deactivating them when needed.

Using pipx for Installing Global CLI Tools

Some tools like black, flake8, or pytest can be installed globally but isolated.

Example

Bash
pip install pipx
pipx install black

This keeps global tools separate from project environments.

Using Virtual Environment Wrapper Tools

Tools like virtualenvwrapper or conda make managing environments easier.

virtualenvwrapper example

Bash
mkvirtualenv myproject
workon myproject

Conda environment example

Bash
conda create -n myproject python=3.10
conda activate myproject

Why Use These Tools?

Best Practices for Managing Python Dependencies

Using pip-tools for Advanced Dependency Management

pip-tools helps manage pinned and unpinned dependency versions.

Install pip-tools

Bash
pip install pip-tools

Use requirements.in

Create a file:

Code
numpy
pandas
flask

Then compile:

Bash
pip-compile

This generates a full requirements.txt with locked versions.

Conclusion

Creating virtual environments and managing dependencies correctly is essential for every Python developer. Using tools like venv, pip, pip-tools, and conda, you can prevent version conflicts, maintain clean project structures, and ensure consistent behavior across machines. With these simple techniques, your Python projects will become easier to manage, maintain, and deploy — whether you're working alone or with a team.