Comprehensive beginner's virtualenv tutorial?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A virtual environment gives one Python project its own interpreter context and installed packages, separate from other projects. That separation is the main reason Python dependency management stays sane once you work on more than one codebase.
Why Beginners Should Use Virtual Environments Early
Without an isolated environment, pip install usually affects a shared Python installation. That creates conflicts when one project needs one package version and another project needs a different one.
A virtual environment fixes that by creating a project-local environment with its own installed packages.
The practical outcome is simple:
- project A can use one dependency version
- project B can use another
- your global Python stays cleaner
venv Versus virtualenv
For modern Python 3 projects, the built-in venv module is usually enough.
virtualenv is an external tool that offers extra features and broader compatibility, and many teams still use it.
For a beginner, the simplest advice is:
- use
python -m venv .venvunless your team or tooling specifically requiresvirtualenv
Creating An Environment
A common workflow is:
This creates a directory named .venv containing the environment.
To activate it on macOS or Linux:
On Windows Command Prompt:
On Windows PowerShell:
After activation, python and pip usually point at the environment instead of the global interpreter.
Installing Packages Inside The Environment
Once the environment is active, install packages normally.
Then verify them:
The important point is that those packages now belong to the active environment, not necessarily to the system Python.
Saving Dependencies
When your environment contains the packages a project needs, save them to a requirements file.
Another developer or deployment environment can recreate the package set with:
This is one of the main benefits of using environments properly: repeatable project setup.
Deactivating And Removing The Environment
When you are done working in the environment, deactivate it:
If you want to remove the environment entirely, delete the .venv directory.
On Windows, remove it through Explorer or the appropriate shell command.
The environment is disposable. That is a feature, not a bug.
A Typical Beginner Workflow
A very practical pattern for each project is:
That is enough to start building with isolated dependencies.
What To Commit To Git
Usually, commit:
- your project code
- '
requirements.txtor another dependency file'
Usually, do not commit:
- the
.venvdirectory
Add the environment directory to .gitignore.
This keeps the repository smaller and avoids platform-specific environment clutter.
Common Pitfalls
The most common mistake is forgetting to activate the environment before installing packages. Then packages end up in the wrong Python installation.
Another mistake is creating multiple environments and losing track of which one is active.
Beginners also sometimes commit the .venv directory to Git, which is usually unnecessary and noisy.
Finally, do not assume virtualenv and venv are the same command. They solve a similar problem, but they are different tools.
Summary
- A virtual environment isolates one project's Python packages from others.
- For modern Python 3 work,
python -m venv .venvis usually the simplest choice. - Activate the environment before installing packages.
- Save dependencies with
pip freeze > requirements.txt. - Do not commit the
.venvdirectory to version control.

