Python Virtual Environment Tutorial: venv, pip and uv
Stop dependency conflicts for good. Learn how to create, activate and manage a Python virtual environment, then try the faster uv tool.
- Create:
python -m venv .venv - Activate:
.venv\Scripts\activate(Windows) orsource .venv/bin/activate(macOS/Linux) - Install with
pip install package; save withpip freeze > requirements.txt - Never commit the environment folder to Git.
uvis a fast alternative for creating environments and installing packages.
You install a package for one project and suddenly another project stops working. That is the classic dependency conflict, and it is why every developer needs a Python virtual environment.
A virtual environment gives each project its own isolated set of packages, so versions never collide. It takes about 30 seconds to set up and saves hours of debugging. In this tutorial you will create and activate one with venv, install packages with pip, save dependencies, try uv and fix common errors.
What is a Python virtual environment?
Without one, pip install puts packages in your global Python. Project A may need requests 2.28 while project B needs a newer version, and only one can win. A virtual environment is a folder with its own interpreter link and its own site-packages, so each project keeps the versions it needs.
Test automation projects pin versions of Selenium, Playwright or pytest so results are repeatable on every laptop and CI server. A virtual environment plus a requirements file is the foundation of that. New to the language? Start with our Python tutorial for beginners.
Create a Python virtual environment with venv
Step 1: check your Python version
python --version
On some systems the command is python3. The venv module ships with modern Python, so no extra install is needed. See the official venv documentation.
Step 2: create the environment
cd my-project
python -m venv .venv
This creates a .venv folder in your project. The name is a convention that many tools and editors detect automatically.
Step 3: activate it
| System | Command |
|---|---|
| Windows (PowerShell) | .venv\Scripts\Activate.ps1 |
| Windows (cmd) | .venv\Scripts\activate.bat |
| macOS / Linux | source .venv/bin/activate |
When active, your prompt shows (.venv). Type deactivate to leave. If PowerShell blocks the script, allow local scripts for your user with Set-ExecutionPolicy -Scope CurrentUser RemoteSigned.
Install packages in your Python virtual environment with pip
pip install requests
pip list
pip install --upgrade requests
Check you are using the environment’s pip with pip --version; the path should point inside .venv. The pip user guide covers more options.
Save and restore dependencies
pip freeze > requirements.txt
# later, on another machine
python -m venv .venv
pip install -r requirements.txt
Commit requirements.txt so teammates and CI can rebuild the same environment. Not sure about commits? Read our Git tutorial for beginners. Add .venv/ to your .gitignore, because environments are large, machine specific and easy to recreate.
Try uv: a faster alternative
uv is a fast Python package and project manager written in Rust. It creates environments and installs packages much faster than plain pip and works with the same requirements.txt files.
uv venv
uv pip install requests
uv pip freeze > requirements.txt
Quick tip: If pip and venv already work for you, you do not need to switch. Consider uv when installs are slow, such as in large CI pipelines, and test it on a branch first.
Using a Python virtual environment in real projects
- VS Code: press Ctrl+Shift+P, choose “Python: Select Interpreter” and pick the
.venvinterpreter. - CI pipelines: create a fresh environment, install from the requirements file and run tests, as in our GitHub Actions CI/CD guide.
- Containers: the same idea applies in images; see our Docker tutorial for beginners.
- API and test scripts: a small requests project in its own environment pairs well with API testing with Postman.
Common errors and fixes
| Problem | Fix |
|---|---|
| “python is not recognized” | Reinstall Python with “Add Python to PATH” ticked, or use the py launcher on Windows |
| Script execution disabled in PowerShell | Use the RemoteSigned command above, or activate via cmd |
| Packages install globally | You forgot to activate; look for (.venv) in the prompt |
| Broken environment after moving the folder | Delete .venv and recreate it from requirements.txt |
| Wrong Python version | Create it with the exact interpreter, such as python3.12 -m venv .venv |
Best practices: one environment per project, name it .venv in the project root, pin dependencies, ignore the folder in Git, recreate rather than repair, and document setup steps in the README.
Final thoughts
A Python virtual environment is the simplest habit that makes projects reproducible. Create one with python -m venv .venv, activate it, install what you need and freeze your dependencies. Once that feels natural, experiment with uv for speed, then put your environment to work on a small automation script or test project.
Frequently asked questions
How do I create a virtual environment in Python?
Run python -m venv .venv in your project folder, then activate it with the script for your operating system.
How do I activate a virtual environment on Windows?
In PowerShell run .venv\Scripts\Activate.ps1; in Command Prompt run .venv\Scripts\activate.bat.
What is the difference between venv and virtualenv?
The venv module is built into Python 3, while virtualenv is a third-party tool with extra features and older-Python support. For most projects, venv is enough.
Should I commit my virtual environment to Git?
No. Commit requirements.txt instead and add the environment folder to .gitignore.
Is uv better than pip?
uv is usually much faster and works with common pip workflows, but pip remains the standard and is fine for most projects.
Keep learning
Need help with testing, WordPress or SEO?
We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.