Techtrekking

How to Create a Python Virtual Environment with venv (Mac, Windows & Ubuntu)

By Pravin

How to Create a Python Virtual Environment with venv (Mac, Windows & Ubuntu)

Managing Python dependencies across projects can quickly become a tangled mess. Virtual environments solve this by giving each project its own isolated Python environment with its own packages. The built-in venv module is all you need — no third-party tools required.


What Is a Virtual Environment?

A virtual environment is an isolated directory that contains:

  • A copy (or symlink) of the Python interpreter
  • Its own pip for installing packages
  • A site-packages folder separate from the system Python

This means packages installed in one project won't interfere with another — or with your system Python.


Prerequisites

  • Python 3.3+ (venv ships with Python 3 by default)
  • A terminal or command prompt

To check your Python version:

python3 --version # macOS / Ubuntu python --version # Windows

Creating a Virtual Environment

macOS

# Navigate to your project folder cd ~/projects/my-project # Create the virtual environment python3 -m venv .venv # Activate it source .venv/bin/activate # Your prompt will change to show (.venv) # Install packages as normal pip install requests # Deactivate when done deactivate

Windows (Command Prompt)

:: Navigate to your project folder cd C:\Users\YourName\projects\my-project :: Create the virtual environment python -m venv .venv :: Activate it .venv\Scripts\activate.bat :: Install packages as normal pip install requests :: Deactivate when done deactivate

Windows (PowerShell)

# You may need to allow scripts to run first (one-time setup) Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser # Navigate to your project folder cd C:\Users\YourName\projects\my-project # Create the virtual environment python -m venv .venv # Activate it .venv\Scripts\Activate.ps1 # Deactivate when done deactivate

Ubuntu / Linux

# Install venv if not already available sudo apt update && sudo apt install python3-venv -y # Navigate to your project folder cd ~/projects/my-project # Create the virtual environment python3 -m venv .venv # Activate it source .venv/bin/activate # Install packages as normal pip install requests # Deactivate when done deactivate

Saving and Restoring Dependencies

Always freeze your dependencies so others (or your future self) can reproduce the environment:

# Save current dependencies pip freeze > requirements.txt # Recreate the environment from scratch python3 -m venv .venv source .venv/bin/activate # or the Windows equivalent pip install -r requirements.txt

Best Practices for Naming Virtual Environments

The name you give your virtual environment matters more than it seems. Here's what the Python community has converged on:

Use .venv (Recommended)

.venv/

Why it's the standard:

  • Recognised by default in VS Code, PyCharm, and most editors — they auto-detect and activate it
  • The leading dot marks it as a "hidden" configuration directory, keeping your project root clean
  • Explicitly listed in the Python Packaging User Guide as the preferred name
  • Works seamlessly with tools like poetry, hatch, and ruff

Add It to .gitignore

Always exclude your virtual environment from version control:

# .gitignore .venv/ venv/ env/ *.egg-info/ __pycache__/

Naming Conventions to Avoid

NameProblem
venvCommon but not hidden; clutters directory listings
envToo generic; often confused with environment variable files
myenv, project_envDescriptive but non-standard; editors won't auto-detect
virtualenvVerbose and tool-specific (not venv)
.envAvoid — collides with .env files used for secrets (dotenv)

One Environment Per Project

Don't share a single virtual environment across multiple projects. Each project gets its own:

projects/
├── web-api/
│   ├── .venv/          ← isolated to web-api
│   ├── main.py
│   └── requirements.txt
└── data-pipeline/
    ├── .venv/          ← isolated to data-pipeline
    ├── pipeline.py
    └── requirements.txt

Keep Environments Local to the Project

Store .venv inside the project directory rather than in a central location (e.g. ~/.virtualenvs/). This makes projects self-contained and easier to delete or move.


Quick Reference

TaskmacOS / UbuntuWindows (PowerShell)
Createpython3 -m venv .venvpython -m venv .venv
Activatesource .venv/bin/activate.venv\Scripts\Activate.ps1
Deactivatedeactivatedeactivate
Check active envwhich pythonwhere python
Freeze depspip freeze > requirements.txtpip freeze > requirements.txt
Install from filepip install -r requirements.txtpip install -r requirements.txt

Summary

  • Use python3 -m venv .venv to create your environment
  • Name it .venv — it's the modern standard and plays nicely with editors and tooling
  • Always add .venv/ to .gitignore
  • Never commit the environment itself, only requirements.txt
  • One virtual environment per project, stored inside the project directory

Once this becomes habit, dependency conflicts become a thing of the past.

Comments
No comments yet. Be the first to comment!
Leave a Comment
Your comment will be visible after approval.