School of Information Systems

Virtual Environments in Python

Virtual Environment (venv) is a feature in Python that allows us to create isolated environments to run specific projects. This means that each project can have its own versions of libraries and dependencies without affecting the main Python system or other projects.

Why is Virtual Environment Important?
✅ Avoid Dependency Conflicts
If project A needs Django 3.2 and project B needs Django 4.0, without venv there can be conflicts. With venv, each project has its own library.
✅ Keep Python System Clean
Packages are only installed in virtual environments, not in the global system.
✅ Ease of Deployment
When uploading a project to the server, we only need to install the libraries in requirements.txt.
✅ Ensure Projects Run with Consistent Python Versions and Libraries

How Does a Virtual Environment Work?
1. Create a Virtual Environment
a) Check Python Version
Before creating a virtual environment, make sure you have Python installed:

b) Create a Virtual Environment
Use the following command:

2. Activate Virtual Environment
Windows :

Linux/Mac :

3. Installing Packages in Virtual Environment

4. Disable Virtual Environment

5. Deleting Virtual Environment

Conclusion
Virtual environments help manage Python project dependencies in isolation.
Use venv to create, enable, and install packages in a virtual environment.
Save dependencies with pip freeze > requirements.txt so they can be replicated in other environments.

Freza Fathur Nur Purnomo