Creating a Python Virtual Environment (venv): A Step-by-Step Guide
- Samul Black
- Jul 23
- 3 min read
Updated: Aug 18
Managing Python dependencies can quickly become overwhelming when juggling multiple projects. Different libraries and versions often lead to compatibility issues—this is where virtual environments come in handy. In this blog, you’ll learn how to create and manage isolated python virtual environment (venv) using the built-in venv module.
Whether you're working on a data science project, web app, or automation script, this guide will help you keep your development setup clean and conflict-free.

What Is a Virtual Environment in Python?
A virtual environment is a self-contained directory that includes its own Python interpreter and a local pip installation, completely isolated from the global Python setup on your system. This means every project can operate in its own dedicated environment with its own dependencies, ensuring clean separation and better project management.
This is especially important when:
You need to work with different versions of the same library across multiple projects
You're contributing to a project that requires specific package versions
You want to avoid breaking existing code by installing new packages globally
You plan to distribute your project and want to document dependencies cleanly
Using virtual environments allows you to:
Use different library versions for each project
Prevent package conflicts and version mismatches
Easily share and recreate project environments using a requirements.txt file
Experiment safely with packages and roll back without affecting your global Python setup
Whether you're developing a machine learning model, building a web application, or automating tasks with scripts, virtual environments provide a controlled and reproducible workspace.
Prerequisites
Python 3.3 or later installed
A terminal or command prompt
Basic command line knowledge
You can check your Python version with:
python3 --version
Output:
Python 3.13.3How to Create a Python Virtual Environment (venv)
Creating a virtual environment in Python allows you to isolate project-specific dependencies and avoid conflicts with global packages. In this quick guide, you’ll learn how to use Python’s built-in venv module to set up a clean, manageable development environment for any Python project.
Step 1: Navigate to Your Project Directory
Open your terminal and move into the folder where you want to set up the project:
cd path/to/your/projectStep 2: Create the Virtual Environment
python3 -m venv envHere, env is the name of your virtual environment folder. You can name it anything you like, but env or .venv are common conventions.
This command creates:
A bin/ or Scripts/ directory with a local Python interpreter
A lib/ directory with site-packages
A pyvenv.cfg file to track the environment
Step 3: Activating the Virtual Environment
On macOS/Linux:
source env/bin/activate
Output:
(env) Samul@Samul-Mac-mini python-env % On Windows (CMD):
env\Scripts\activateOn Windows PowerShell:
.\env\Scripts\Activate.ps1Once activated, your shell prompt should be prefixed with (env)—indicating that you're working inside the virtual environment.
Step 4: Installing Packages Inside the Environment
Once inside your venv, use pip like usual. For example:
pip install numpy pandas flask
Output:
...
Collecting werkzeug>=3.1.0 (from flask)
Downloading werkzeug-3.1.3-py3-none-any.whl.metadata (3.7 kB)
Collecting six>=1.5 (from python-dateutil>=2.8.2->pandas)
Downloading six-1.17.0-py2.py3-none-any.whl.metadata (1.7 kB)
Downloading numpy-2.3.1-cp313-cp313-macosx_14_0_arm64.whl (5.1 MB)
━━━━━━━━━━━━━━━━━━╺━━━━━━━━━━━━━━━━━━━━━ 2.4/5.1 MB 102.0 kB/s eta 0:00:27
...To list installed packages:
pip list
Output:
Package Version
--------------- -----------
blinker 1.9.0
click 8.2.1
Flask 3.1.1
itsdangerous 2.2.0
Jinja2 3.1.6
MarkupSafe 3.0.2
numpy 2.3.1
pandas 2.3.1
pip 25.0.1
python-dateutil 2.9.0.post0
pytz 2025.2
six 1.17.0
tzdata 2025.2
Werkzeug 3.1.3
To freeze dependencies to a file:
pip freeze > requirements.txtTo install packages from that file later:
pip install -r requirements.txtStep 5: Deactivating the Virtual Environment
To exit the virtual environment and return to your system Python:
deactivateOptional: Ignore env/ in Git Projects
Add this line to your .gitignore file so your environment folder isn't tracked in version control:
env/This keeps your Git repositories clean and avoids pushing unnecessary binaries.
Why Use venv Instead of Global Python?
Scenario | Without venv | With venv |
Package conflict | High risk | Isolated per project |
Dependency sharing | Manual | Easy with requirements.txt |
Clean uninstall | Tedious | Just delete the env/ folder |
Team collaboration | Risky | Safe and predictable |
Few things to consider:
You can create multiple environments for different Python versions by using pyenv or tools like conda.
Use .venv instead of env to keep it hidden from most file explorers and IDEs.
Python IDEs like VS Code, PyCharm, and JupyterLab automatically detect virtual environments in your project.
Conclusion
Using a virtual environment in Python is a recommended best practice for all types of projects—from small automation scripts to full-scale production applications. With just a few simple commands, you can isolate project dependencies, maintain a clean workspace, and avoid version conflicts that often arise from sharing libraries across projects.
Whether you're building web applications with Flask, Django, or FastAPI, or diving into data science with Pandas, NumPy, or Scikit-learn, setting up a virtual environment is the essential first step toward a more stable, reproducible, and scalable Python development workflow.

