Create the environment
python -m venv venv
This makes a folder called venv holding a private Python and pip just for this project.
Activate it
# macOS and Linux
source venv/bin/activate
# Windows (PowerShell)
venvScriptsActivate.ps1
Once active, your prompt shows (venv) and every pip install goes into this environment only.
Install packages and save them
pip install requests
pip freeze > requirements.txt
pip freeze writes the exact versions to a file so others can recreate your setup.
Deactivate when you are done
deactivate
This returns you to your system Python without deleting anything.
Common mistakes
- Committing the venv folder to git. Add
venv/to.gitignoreand sharerequirements.txtinstead. - Forgetting to activate. If packages seem missing, check that
(venv)shows in your prompt.
Frequently asked questions
Why use a virtual environment at all?
It isolates each project’s dependencies, so two projects can use different versions of the same library without conflict.
Can I name it something other than venv?
Yes — python -m venv myenv works fine, but venv is the common convention that most tools expect to ignore.
Set up your first real project the right way with our free Python course.