A Virtual Environment is an isolated workspace that allows you to install libraries specific to one project without interfering with other projects. Imagine it like having several different toolboxes for every different home repair job.
For beginners, Virtual Environment is crucial because often one project requires different package versions than another project. Without a virtual environment, you risk breaking running programs by installing new packages. By using a virtual environment, your computer system stays clean and every project only has access to the tools it truly needs.
There are several ways to create a Virtual Environment in Python. Let's discuss the most common ones and the newest ones.
Using Built-in Module: venv
Python comes equipped with venv module. This is the most common and standard way.
1. How to Create venv
Open terminal or command prompt in your project directory, then run:
# Windows
python -m venv myenv
# macOS / Linux
python3 -m venv myenv
myenv is the name of the folder that will contain your virtual environment.
2. How to Activate venv
Once created, you must activate it:
# Windows
myenv\Scripts\activate
# macOS / Linux
source myenv/bin/activate
Once active, you will see (myenv) in front of your terminal prompt.
3. How to Deactivate
To exit from the virtual environment, simply type:
deactivate
Modern Option: uv from Astral
If you want something much faster and modern, uv is the best choice right now. uv is a Python package and environment manager written in Rust, which speed is 10x to 100x faster than traditional tools.
1. Installing uv
If you don't have it yet, install it first (using pip or official installer):
pip install uv
2. Creating and Using Environment with uv
uv makes environment management highly automated:
# Create environment
uv venv
# Activate (same as normal venv)
.venv\Scripts\activate # Windows
source .venv/bin/activate # macOS/Linux
# Install packages very fast
uv pip install requests
One of uv's advantages is its ability to manage Python versions itself without needing to manually install from Python website.
Importance in the Professional World
In the professional work world, Virtual Environment is no longer an option, but a mandatory standard. When working in large teams or managing systems on cloud servers, you must ensure that the application you build has a clear dependency list and does not clash with other applications. This ensures the principle of reproducibility, where your colleagues can run the exact same code with the exact same results on their computers.
Additionally, Virtual Environment facilitates the deployment process. When the application is ready to be shipped to production server, you simply provide the list of packages (usually in requirements.txt file) that are inside that virtual environment. Without this tool, moving code from developer's computer to server would be a technical nightmare full of errors due to library version differences.
Gabung Komunitas Developer & Kreator Digital
Dapatkan teman coding, sharing project, networking dengan expert, dan update teknologi terbaru.
Selamat! Anda telah sukses mendaftar di newsletter.