Whether you're building data pipelines, web apps, or experimenting with AI models, managing project dependencies is crucial. Thatβs where Python virtual environments come into play.
π Table of Contents
- π What is a Python Virtual Environment?
- βοΈ Prerequisites
- π» Virtual Environment Setup by OS
- π Virtual Environment Directory Structure
- β Steps to Setup a Project
- π§Ύ Sample Program: main.py
- π‘ Bonus Tips
- π Conclusion
π What is a Python Virtual Environment?
A virtual environment is an isolated environment for Python projects. It allows you to manage dependencies separately for each project, preventing conflicts between libraries.
A virtual environment helps you keep each Python project separate on the same computer.
It lets you use specific versions of packages for each project without interfering with other projects or your system.
This logical separation makes it easy to manage multiple projects with different requirements at the same time.
βοΈ Prerequisites
- Ensure Python is installed:
python --version # or python3 --version
If not, download Python.
π» Virtual Environment Setup by OS
π₯ Windows
- Open Command Prompt or PowerShell
- Create a virtual environment
python -m venv env
Replace
env
with your desired environment name.
.
π§ Creating a Python Virtual Environment with a Specific Version
Setting up a virtual environment with a specific Python version ensures your project dependencies remain isolated and compatible. Below are different ways to create a Environment using the version you want:
- β Using python3.x (Linux/macOS)
python3.10 -m venv venv
This creates a virtual environment named venv using Python 3.10 (if installed).
- β Using py -3.x (Windows)
py -3.10 -m venv venv
This is the preferred way on Windows when managing multiple Python versions via the Python Launcher (py).
- Activate the environment
.\env\Scripts\activate
or
- Command Prompt :
env\Scripts\activate.bat
- Powershell :
env\Scripts\Activate.ps1
π macOS / π§ Linux
- Open Terminal
- Create the virtual environment
python3 -m venv env
- Activate the environment
source env/bin/activate
π§ͺ Verify the Environment
-
which python
# Linux/macOS -
where python
# Windows
You should see the path pointing to your local
env
folder.
π Virtual Environment Directory Structure
project-folder/
β
βββ env/ # Virtual environment directory
β βββ bin/ or Scripts/ # Executables (python, pip)
β βββ include/ # Header files
β βββ lib/ # Installed packages
β βββ pyvenv.cfg # Config file
β
βββ main.py # Your Python script
βββ requirements.txt # Project dependencies (optional)
πΎ Steps to Setup a Project
- Create a project folder
mkdir myproject && cd myproject
- Create the virtual environment
python -m venv env
- Activate it
### Windows
.\env\Scripts\activate
### macOS/Linux
source env/bin/activate
- Install packages
pip install requests
- Freeze requirements
pip freeze > requirements.txt
- Run your Python program
python main.py
After you activate environment it will show as shown in image:
π§Ύ Sample Program: main.py
import requests
def getpost():
# Make a GET request to a public API
url = "https://jsonplaceholder.typicode.com/posts/1"
response = requests.get(url)
# Print the response content Json
#print(response.json())
if response.status_code == 200:
print("Request was successful!")
data = response.json()
print("User ID{user_id} Title: {title} Body: {body}".format(user_id=data['userId'], title=data['title'], body=data['body']))
else:
print("Request failed with status code:", response.status_code)
# Check the status code
print(f"Status Code: {response.status_code}")
if __name__ == "__main__":
getpost()
π‘ Bonus Tips
- To deactivate the environment:
deactivate
- To delete the virtual environment:
rm -rf env # macOS/Linux
rmdir /s env # Windows
- Use requirements.txt to share dependencies with teammates:
pip install -r requirements.txt
Conclusion
Using a virtual environment keeps your Python projects clean and manageable. Itβs an essential skill for every developer.
One does not simply pip install without a virtual environment
Top comments (0)