Why Every Python Dev Needs Virtual Environments Now
A virtual environment is an isolated sandbox that allows you to install everything you need for a project without affecting things globally. Python contains everything you need to create and use a virtual environment, and it's easy.
Jan 9th, 2025 7:30am by
Featured image via Unsplash.
What Are Virtual Environments?
A virtual environment is an isolated sandbox that allows you to install everything you need for a project without affecting things globally. Within a virtual environment, you can install all of the libraries and dependencies you need without touching the global Python installation. Think of Python Virtual Environments like a virtual machine. If you’ve ever used a tool like VirtualBox, you know that host and guest do not affect one another, and anything you do to a guest OS will have no bearing on the host OS. You could effectively install a Linux guest OS, log in, run the sudo rm -rf / command (don’t do that) and it would destroy the guest OS but not touch the host. Python Virtual Environments work in a similar fashion and offer the following benefits:- They allow you to work on multiple projects with different dependencies at the same time.
- They allow you to create portable projects.
- There’s no risk of version conflicts.
- They avoid the need for global package installation.
- They make it easier for testing.
- They make clean up easier.
- They simplify collaboration.
- They are more easily reproducible.
- They offer dependency isolation.
- They create a much cleaner and organized workspace.
What You’ll Need
The only thing you’ll need for this is to install Python on your OS of choice. I’ll demonstrate this on Pop!_OS Linux with Python version 3.10.12, but the process is the same, regardless of the operating system. Do note that as long as you’re using Python version 3.4 and up, it has everything you need to do this. If you’re using a version of Python older than 3.4, I suggest you upgrade; otherwise, you’ll need to install virtualenv using Pip (pip install virtualenv).Creating a Virtual Environment
The first thing you want to do is create a new virtual environment. Log in to your OS and open a terminal window. Once you have access to the CLI, create a directory to house your Python projects like so:
mkdir PYTHON
cd PYTHON
python -m venv ProjectX
sudo apt-get instll python3.10-venv
virtualenv ProjectX
- bin
- include
- lib
- lib64
cd ProjectX
source bin/activate
(ProjectX) hostname ->
- For cmd.exe – venv\Scripts\activate.bat
- For PowerShell – venv\Scripts\Activate.ps1
Deactivating a Project
When you’re finished working on a project, it’s good practice to deactivate it. This will leave the virtual environment intact but prevent anything from happening to it. To deactivate a virtual environment, type the following command within the project directory:
deactivate
Deleting a Virtual Environment
If you need to delete that virtual environment, all you have to do is deactivate it and then delete the directory with the following commands: If you’re working on a Windows environment, you’ll need to change the last two commands to use the Windows equivalent. And that, my friends, is all there is to working with a Python virtual environment.
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.