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:
py -m venv .venv
.\.venv\Scripts\Activate.ps1macOS:
python3 -m venv .venv
source .venv/bin/activateLast verified: June 9, 2026.
Official Links
- Python
venvdocs: https://docs.python.org/3/library/venv.html - Python tutorial on virtual environments: https://docs.python.org/3/tutorial/venv.html
- Python packaging guide: https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/
- pip docs: https://pip.pypa.io/en/stable/
What You Will Learn
By the end, you should be able to:
- explain why virtual environments matter
- create a
.venvfolder - 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:
project-a needs pandas 2.3
project-b needs pandas 2.0If both projects use one global Python, packages can conflict.
With virtual environments:
project-a/.venv has project-a packages
project-b/.venv has project-b packagesThe environment is disposable. Your code is important. .venv can always be deleted and recreated.
2. Create A Project Folder
Windows PowerShell:
mkdir python-venv-practice
cd python-venv-practicemacOS:
mkdir python-venv-practice
cd python-venv-practice3. Create The Virtual Environment
Windows:
py -m venv .venvmacOS:
python3 -m venv .venvLinux:
python3 -m venv .venv.venv is the common folder name. Do not put your source code inside .venv.
4. Activate The Virtual Environment
| Platform | Shell | Activation command |
|---|---|---|
| Windows | PowerShell | .\.venv\Scripts\Activate.ps1 |
| Windows | Command Prompt | .venv\Scripts\activate.bat |
| macOS | zsh/bash | source .venv/bin/activate |
| Linux | bash/zsh | source .venv/bin/activate |
| macOS/Linux | fish | source .venv/bin/activate.fish |
After activation, your prompt often begins with:
(.venv)5. Verify The Environment
After activation, run:
python -c "import sys; print(sys.executable)"
python -m pip --versionExpected result:
- the Python path includes
.venv - pip also points inside
.venv
On Windows, the path may include:
.venv\Scripts\python.exeOn macOS, the path may include:
.venv/bin/python6. Install Packages Inside The Environment
After activation:
python -m pip install pandas matplotlibTest:
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:
deactivateThis removes .venv from the active terminal PATH.
8. Create requirements.txt
After installing packages:
python -m pip freeze > requirements.txtExample:
pandas==2.3.0
matplotlib==3.10.0Commit requirements.txt to Git.
9. Recreate The Environment
If .venv is deleted or someone else downloads your project, recreate it.
Windows:
py -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install -r requirements.txtmacOS:
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt10. What Goes Into Git?
Commit:
your_code.py
requirements.txt
README.md
data/sample.csvDo not commit:
.venv/
__pycache__/
.envBeginner .gitignore:
.venv/
__pycache__/
*.pyc
.env11. PowerShell Activation Error
If PowerShell says scripts are disabled, run:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUserThen close and reopen PowerShell.
Activate again:
.\.venv\Scripts\Activate.ps1This changes policy only for your current Windows user.
12. macOS PATH Or Activation Problems
If activation fails, confirm the folder exists:
ls .venv/binIf python3 -m venv .venv fails, check Python:
python3 --version
which python3If the terminal cannot find newly installed tools, restart Terminal or reload zsh config:
source ~/.zshrc13. 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:
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:
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
deactivatemacOS:
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
deactivateIf the Python path includes .venv, the setup is correct.
Next in this series: Getting Started with uv for Python: Installation and Project Setup →
