A virtual environment is an isolated workspace within your Python installation on your computer. It allows you to manage package installations, dependencies, and unique configurations for each project, ensuring that packages from one project don’t interfere with others. Think of it as a completely separate installation where you can write code, manage projects, experiment, and develop safely—without altering or affecting the global Python installation on your system.
What Are the Advantages of Working with a Virtual Environment?
- Isolation: Virtual environments provide isolation for projects, preventing conflicts between dependencies of different projects. What’s installed in one environment stays there, allowing clean and conflict-free development.
- Dependency Management: Managing dependencies becomes straightforward. You can specify exact versions of libraries required for a project, ensuring unparalleled consistency and stability.
- Project Portability: Virtual environments make it easier to share your code with others. By including a
requirements.txtfile that lists the project’s dependencies, the same environment can be easily replicated. - Experimentation: You can freely experiment within a virtual environment, knowing that any changes you make won’t affect the system-wide Python installation.
- Security: Installing packages globally increases the system’s attack surface and can expose it to risks. A virtual environment isolates packages, preventing them from accessing global parameters or affecting the entire system. This allows for safe development and experimentation without jeopardizing system stability or other projects.
- Easy Removal: Want to clean up your computer? Something went wrong? No problem. Just delete the environment’s folder and move on. It’s that simple.
How to Get Started?
Step 1 – Check Python Version
Assuming Python is already installed on your computer, note that referencing Python may vary across operating systems (Windows, macOS, Linux). First, verify the installed Python version by opening CMD or Terminal and typing:
python --version
Linux users should refer to Python with the major version number:
python3 --version
Step 2 – Create a Project Directory
After confirming the version, create the directory where the environment will reside. This step is for Windows users only:
mkdir development # Create the directory
cd development # Navigate into the directory
Step 3 – Setup
Now, create the virtual environment.
- Windows Users: Within the directory you created (and navigated into in the previous step), run:
python -m venv {ENV_NAME}
- Linux and macOS Users: No need to create a special directory, as the command itself creates the virtual environment folder. Navigate to the directory where you want to set up the environment and run:
python3.12 -m venv {ENV_NAME}
Replace {ENV_NAME} with your desired environment name.
Step 4 – Activate the Environment
Now, activate the environment. Note that the command differs between Windows and Linux/macOS users.
- Windows Users:
{ENV_NAME}\Scripts\activate
- Linux and macOS Users:
source {ENV_NAME}/bin/activate
Remember to replace {ENV_NAME} with the name of the environment you chose.
Step 5 – Install Dependencies
After setting up and activating the environment, you’ll likely want to install dependencies within the environment. It’s straightforward—just run the pip install command:
pip install pandas jupyter-lab tensorflow etc.
It’s important to remember that installing dependencies is done at the virtual environment level only, not system-wide—reducing the attack surface and limiting the potential harm of malicious packages.
Deactivate the Virtual Environment
By running the exit or deactivate command, you can deactivate the environment.
Delete the Environment
Familiar with the phrase “trash it and forget it”? That’s exactly it. Delete the folder and move on. That’s all.
In Summary
Creating a virtual environment using Python’s built-in venv module offers numerous significant advantages for developers. It allows complete project isolation, precise dependency management, improved collaboration, and a safe space for experimentation—all while maintaining a higher level of security. Still installing dependencies system-wide? After reading this article, it’s highly recommended to reconsider that habit and switch to proper and organized work with virtual environments.



![[Explaining] Numpy and PyPlot](https://dolevravid.com/wp-content/uploads/2025/04/campaign-creators-pypeceajezy-unsplash.jpg?w=1000)



Leave a comment