Python Virtual Environments: Setup and Usage Guide

Jun 10, 2026
44 min read

AI Insights

Powered by GPT-4o-mini

Verified Context: python-virtual-environments-setup-and-usage-guide
Quick Answer

Learn Python virtual environments with venv: create .venv, activate it, install packages, freeze requirements, recreate environments, and avoid dependency conflicts.

Quick Summary

Learn how to create and manage Python virtual environments with venv, activate them, and handle requirements.txt for your projects.

Python Virtual Environments: venv, Activation, pip, and requirements.txt

Quick Answer

A virtual environment is a private Python environment for one project.

Use one .venv per project.

Windows:

powershell
py -m venv .venv
.\.venv\Scripts\Activate.ps1

macOS:

bash
python3 -m venv .venv
source .venv/bin/activate

Last verified: June 9, 2026.

What You Will Learn

By the end, you should be able to:

  • explain why virtual environments matter
  • create a .venv folder
  • activate and deactivate environments on Windows and macOS
  • install packages inside the environment
  • create requirements.txt
  • recreate an environment from scratch
  • know what should and should not go into Git

1. Why Virtual Environments Exist

Different projects need different packages.

Example:

text
project-a needs pandas 2.3
project-b needs pandas 2.0

If both projects use one global Python, packages can conflict.

With virtual environments:

text
project-a/.venv has project-a packages
project-b/.venv has project-b packages

The environment is disposable. Your code is important. .venv can always be deleted and recreated.

2. Create A Project Folder

Windows PowerShell:

powershell
mkdir python-venv-practice
cd python-venv-practice

macOS:

bash
mkdir python-venv-practice
cd python-venv-practice

3. Create The Virtual Environment

Windows:

powershell
py -m venv .venv

macOS:

bash
python3 -m venv .venv

Linux:

bash
python3 -m venv .venv

.venv is the common folder name. Do not put your source code inside .venv.

4. Activate The Virtual Environment

PlatformShellActivation command
WindowsPowerShell.\.venv\Scripts\Activate.ps1
WindowsCommand Prompt.venv\Scripts\activate.bat
macOSzsh/bashsource .venv/bin/activate
Linuxbash/zshsource .venv/bin/activate
macOS/Linuxfishsource .venv/bin/activate.fish

After activation, your prompt often begins with:

text
(.venv)

5. Verify The Environment

After activation, run:

bash
python -c "import sys; print(sys.executable)"
python -m pip --version

Expected result:

  • the Python path includes .venv
  • pip also points inside .venv

On Windows, the path may include:

text
.venv\Scripts\python.exe

On macOS, the path may include:

text
.venv/bin/python

6. Install Packages Inside The Environment

After activation:

bash
python -m pip install pandas matplotlib

Test:

bash
python -c "import pandas as pd; print(pd.__version__)"

The packages are installed into .venv, not into your global Python.

7. Deactivate

To leave the environment:

bash
deactivate

This removes .venv from the active terminal PATH.

8. Create requirements.txt

After installing packages:

bash
python -m pip freeze > requirements.txt

Example:

text
pandas==2.3.0
matplotlib==3.10.0

Commit requirements.txt to Git.

9. Recreate The Environment

If .venv is deleted or someone else downloads your project, recreate it.

Windows:

powershell
py -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install -r requirements.txt

macOS:

bash
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt

10. What Goes Into Git?

Commit:

text
your_code.py
requirements.txt
README.md
data/sample.csv

Do not commit:

text
.venv/
__pycache__/
.env

Beginner .gitignore:

gitignore
.venv/
__pycache__/
*.pyc
.env

11. PowerShell Activation Error

If PowerShell says scripts are disabled, run:

powershell
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Then close and reopen PowerShell.

Activate again:

powershell
.\.venv\Scripts\Activate.ps1

This changes policy only for your current Windows user.

12. macOS PATH Or Activation Problems

If activation fails, confirm the folder exists:

bash
ls .venv/bin

If python3 -m venv .venv fails, check Python:

bash
python3 --version
which python3

If the terminal cannot find newly installed tools, restart Terminal or reload zsh config:

bash
source ~/.zshrc

13. Should You Use venv Or uv?

Use venv when:

  • you are learning Python fundamentals
  • a course asks for plain Python tooling
  • you want to understand environments clearly

Use uv when:

  • you want faster project setup
  • you want lockfiles
  • you want one tool for Python versions, environments, and packages

uv is covered in the next post.

Practice Lab

Practice Task

Create an environment, install pandas, and run this file:

python
import pandas as pd

scores = pd.DataFrame({
    "name": ["Asha", "Ben", "Chen"],
    "score": [88, 91, 79],
})

print(scores)
print("Average:", scores["score"].mean())

If it runs, your environment works.

FAQ

Should I create .venv inside every project?

Yes. It keeps project dependencies separate and easy to recreate.

Should I commit .venv to Git?

No. Commit requirements.txt or pyproject.toml, then recreate .venv later.

Do I need to activate the environment every time?

For terminal work, yes. IDEs can also select the .venv interpreter directly.

Can I move a .venv folder to another computer?

Do not rely on that. Recreate it from requirements.txt.

Final Checklist

Windows:

powershell
py -m venv .venv
.\.venv\Scripts\Activate.ps1
python -c "import sys; print(sys.executable)"
python -m pip install requests
python -m pip freeze > requirements.txt
deactivate

macOS:

bash
python3 -m venv .venv
source .venv/bin/activate
python -c "import sys; print(sys.executable)"
python -m pip install requests
python -m pip freeze > requirements.txt
deactivate

If the Python path includes .venv, the setup is correct.


Next in this series: Getting Started with uv for Python: Installation and Project Setup →

Frequently Asked Questions

What is a Python virtual environment?
A virtual environment is a private Python environment for one project, allowing different projects to have different package dependencies without conflicts.
How do you activate a virtual environment on Windows using PowerShell?
To activate a virtual environment on Windows using PowerShell, use the command: .\.venv\Scripts\Activate.ps1.
Why are virtual environments important?
Virtual environments are important because different projects may require different package versions, and using a global Python environment can lead to package conflicts.
How can you verify that a virtual environment is activated?
After activation, you can verify by running 'python -c "import sys; print(sys.executable)"' and checking that the Python path includes .venv.
What should you not put inside the .venv folder?
You should not put your source code inside the .venv folder.

Related Work

See how this thinking shows up in shipped systems.