A Python environment is an isolated workspace that contains:
- A specific Python interpreter version
- A set of installed packages/libraries
- Its own
site-packagesdirectory
Problem without environments:
Project A needs Django 3.2
Project B needs Django 4.2
→ You can't install both globally → They conflict!
Solution with virtual environments:
Project A → its own venv → Django 3.2 installed here
Project B → its own venv → Django 4.2 installed here
Both work independently, no conflicts!
| Type | Tool | Use Case |
|---|---|---|
| Virtual Environment | venv (built-in) |
Standard Python projects |
| Conda Environment | conda |
Data science, scientific computing |
| Pipenv | pipenv |
Combined pip + virtualenv |
| Poetry | poetry |
Modern dependency management |
# Windows
python -m venv myenv
# Mac/Linux
python3 -m venv myenv
# Create with specific Python version
py -3.11 -m venv myenvThis creates a folder myenv/ with:
myenv/
├── Scripts/ (Windows) or bin/ (Mac/Linux)
│ ├── python.exe
│ ├── pip.exe
│ └── activate
├── Lib/
│ └── site-packages/ ← your installed packages go here
└── pyvenv.cfg
# Windows (Command Prompt)
myenv\Scripts\activate
# Windows (PowerShell)
myenv\Scripts\Activate.ps1
# Mac/Linux
source myenv/bin/activate
# After activation, your prompt changes to:
(myenv) C:\Users\Logesh\project># Install packages (only goes into this venv)
pip install requests flask numpy
# Check what's installed
pip list
# Save dependencies to a file
pip freeze > requirements.txt# Another developer clones your project and runs:
pip install -r requirements.txtdeactivate
# Prompt returns to normal# Windows
rmdir /s myenv
# Mac/Linux
rm -rf myenv# 1. Create project folder
mkdir my_project
cd my_project
# 2. Create virtual environment
python -m venv venv
# 3. Activate it
venv\Scripts\activate # Windows
# source venv/bin/activate # Mac/Linux
# 4. Install dependencies
pip install flask requests
# 5. Save them
pip freeze > requirements.txt
# 6. Write your code
# (create your Python files)
# 7. Deactivate when done
deactivate# Create conda environment
conda create --name myenv python=3.11
# Activate
conda activate myenv
# Install packages
conda install numpy pandas matplotlib
# List environments
conda env list
# Deactivate
conda deactivate
# Export environment
conda env export > environment.yml
# Create from file
conda env create -f environment.yml- Open PyCharm → New Project
- Under "Python Interpreter" → select "New environment using Virtualenv"
- Choose base interpreter (Python version)
- Click Create
- PyCharm auto-activates the venv for terminal sessions
# .gitignore
venv/
__pycache__/
*.pyc
.envNever commit your
venv/folder to Git! Sharerequirements.txtinstead.
# Install a package
pip install package_name
# Install specific version
pip install flask==2.3.0
# Install minimum version
pip install flask>=2.0.0
# Upgrade a package
pip install --upgrade flask
# Uninstall a package
pip uninstall flask
# Search for packages
pip search flask # (deprecated, use PyPI website)
# Show package info
pip show flask
# List all installed packages
pip list
# List packages not in requirements
pip checkpip install python-dotenv# .env file (never commit this!)
DATABASE_URL=mongodb://localhost:27017
SECRET_KEY=my_super_secret_key
DEBUG=True
# main.py
from dotenv import load_dotenv
import os
load_dotenv() # loads .env file
db_url = os.getenv("DATABASE_URL")
secret = os.getenv("SECRET_KEY")
debug = os.getenv("DEBUG", "False") == "True" # with default
print(f"Connecting to: {db_url}")
print(f"Debug mode: {debug}")Q: Why should you use a virtual environment?
A: To isolate project dependencies so different projects can use different versions of the same library without conflicts. Without venvs, all packages install globally and can break other projects.
Q: What is the difference between pip freeze and pip list?
A:
pip listshows packages in a human-readable format.pip freezeoutputs them inpackage==versionformat suitable forrequirements.txt. You usepip freeze > requirements.txtto save dependencies.
Q: What is the difference between venv and conda?
A:
venvis Python's built-in tool for creating isolated Python environments.condais from Anaconda and manages both Python packages AND non-Python dependencies (like C libraries), making it better for data science/scientific computing.
Q: Should you commit the venv folder to Git?
A: No! The venv folder is large and platform-specific. Instead, commit
requirements.txt(orenvironment.ymlfor conda). Teammates can recreate the environment withpip install -r requirements.txt.
| Concept | Command |
|---|---|
| Create venv | python -m venv venv |
| Activate (Windows) | venv\Scripts\activate |
| Activate (Mac/Linux) | source venv/bin/activate |
| Install package | pip install package |
| Save dependencies | pip freeze > requirements.txt |
| Install from file | pip install -r requirements.txt |
| Deactivate | deactivate |