Category: python
What is a Python Virtual Environment?
Published on 14 Aug 2026
Explanation
A Python virtual environment is an isolated environment created for a specific Python project. It allows a project to have its own Python packages and package versions without affecting other projects on the same computer. For example, one project may require requests version 2.x while another project requires a different version. Virtual environments prevent these dependency conflicts. Python provides the built-in venv module to create virtual environments. Using a virtual environment is a recommended practice for almost every Python project, especially when working with frameworks and third-party libraries.
Code:
python -m venv venv # After creating the environment, # activate it before installing packages. # Windows: # venv\Scripts\activate # macOS/Linux: # source venv/bin/activate
Explanation
The venv module can be used to create an isolated Python environment. The command python -m venv venv creates a folder named venv containing the environment. After creating it, the environment must be activated. Once activated, packages installed using pip are installed inside the virtual environment instead of globally. You can verify the active environment by checking the Python or pip path. When the project work is finished, the deactivate command can be used to leave the virtual environment.
Code:
# Create virtual environment python -m venv venv # Windows Command Prompt venv\Scripts\activate # Windows PowerShell venv\Scripts\Activate.ps1 # macOS/Linux source venv/bin/activate # Check Python version python --version # Deactivate environment deactivate
Explanation
After activating a virtual environment, packages can be installed using pip. The installed packages are isolated from other Python projects. The pip list command shows installed packages, while pip freeze generates a list of exact package versions. This list can be saved in requirements.txt. Another developer or a deployment server can then install the same dependencies using pip install -r requirements.txt. This makes Python projects easier to reproduce and deploy consistently.
Code:
# Activate the virtual environment first # Install packages pip install requests pip install psycopg # View installed packages pip list # Save dependencies pip freeze > requirements.txt # Install dependencies later pip install -r requirements.txt
Explanation
A good project structure organizes Python source code, tests, configuration, dependencies, and documentation into separate locations. As an application grows, keeping everything in one Python file becomes difficult to maintain. A common structure contains an application package, tests directory, requirements.txt file, README.md file, and a virtual environment. The __init__.py file can be used to identify a Python package. Separating responsibilities into multiple modules makes the project easier to understand, test, maintain, and extend.
Code:
myproject/ │ ├── venv/ │ ├── app/ │ ├── __init__.py │ ├── main.py │ ├── models.py │ ├── services.py │ └── utils.py │ ├── tests/ │ ├── __init__.py │ └── test_services.py │ ├── requirements.txt ├── README.md └── .gitignore
Explanation
A Python project can be organized by separating the application entry point from reusable business logic. In this example, main.py imports a function from services.py and uses it. This approach demonstrates modular programming and gives the project a structure that can grow as more functionality is added. The virtual environment, requirements.txt, and .gitignore files support dependency management and source-code management. This structure is a useful foundation before moving to larger frameworks such as FastAPI, Flask, or Django.
Code:
# app/services.py
def calculate_total(price, quantity):
return price * quantity
# app/main.py
from services import calculate_total
price = 500
quantity = 3
total = calculate_total(price, quantity)
print('Total:', total)
# requirements.txt
# requests==2.32.4
# psycopg==3.2.9
# .gitignore
# venv/
# __pycache__/
# .env