Python Virtual Environment
What is a Virtual Environment?
A virtual environment in Python is an isolated environment on your computer, where you can run and test your Python projects.
It allows you to manage project-specific dependencies without interfering with other projects or the original Python installation.
Think of a virtual environment as a separate container for each Python project. Each container:
- Has its own Python interpreter
- Has its own set of installed packages
- Is isolated from other virtual environments
- Can have different versions of the same package
Using virtual environments is important because:
- It prevents package version conflicts between projects
- Makes projects more portable and reproducible
- Keeps your system Python installation clean
- Allows testing with different Python versions
Creating a Virtual Environment
Python has the built-in venv module for creating virtual environments.
python -m venv .venv
Activate Virtual Environment
source .venv/bin/activate
After activation, your prompt will change to show that you are now working in the active environment:
(.venv) ... $
Install Packages
Once your virtual environment is activated, you can install packages in it, using pip
(.venv) ... $ pip install cowsay
Deactivate Virtual Environment
(.venv) ... $ deactivate
Delete Virtual Environment
Another nice thing about working with a virtual environment is that when you, for some reason want to delete it, there are no other projects depend on it, and only the modules and files in the specified virtual environment are deleted.
To delete a virtual environment, you can simply delete its folder with all its content. Either directly in the file system, or use the command line interface like this:
rm -rf .venv