Comprehensive beginner's virtualenv tutorial?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.
Related reading
- Compute list difference
- Compute rolling maximum drawdown of pandas Series
- Compute the DateTime of an upcoming weekday
- Computing generalized eigen values for sparse matrices in python
- Comsuming MassTransit from Python or other languages
- Concatenate a list of pandas dataframes together
- Concatenate a NumPy array to another NumPy array
- Concatenate strings from several rows using Pandas groupby
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.