A Virtual Environment is an isolated workspace for a Python project. It allows you to install specific versions of libraries (like NumPy or Pandas) for one project without affecting other projects or the global Python installation on your computer.
Imagine you are working on two projects:
Project A: Uses an old version of a library (e.g., Django 2.0).
Project B: Uses the latest version (e.g., Django 5.0).
If you install these globally, one will overwrite the other, breaking your code. A Virtual Environment solves this by creating a separate folder that contains its own copy of the Python executable and its own site-packages (where libraries are stored).
Dependency Management: Avoid "dependency hell" where different projects require conflicting library versions.
Reproducibility: You can generate a list of all requirements (using requirements.txt) so another developer can recreate your exact environment.
System Protection: Prevents you from accidentally breaking the Python version used by your Operating System (especially critical on Linux and macOS).
Python comes with a built-in module called venv to handle this.
Open your terminal or command prompt inside your project folder and run:
# 'myenv' is the name of the folder that will be created
python -m venv myenv
You must "enter" the environment to use it. The command varies by Operating System:
Windows: myenv\Scripts\activate
macOS / Linux: source myenv/bin/activate
Once activated, you will usually see (myenv) appear in parentheses at the start of your command prompt.
Now, any package you install via pip stays inside this environment.
pip install requests
When you are done working, simply type:
deactivate
To share your project with others, you record your environment's state into a text file.
# Save all installed libraries to a file pip freeze > requirements.txt # Someone else can install everything at once using: pip install -r requirements.txt
| Action | Command (General) |
| Create | python -m venv <env_name> |
| Activate (Windows) | <env_name>\Scripts\activate |
| Activate (Mac/Linux) | source <env_name>/bin/activate |
| Check Installed | pip list |
| Exit | deactivate |
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION