Skip to content

Latest commit

 

History

History
280 lines (205 loc) · 6.1 KB

02_Environment.md

File metadata and controls

280 lines (205 loc) · 6.1 KB

Day 2 — Python Environment


1. What is a Python Environment?

A Python environment is an isolated workspace that contains:

  • A specific Python interpreter version
  • A set of installed packages/libraries
  • Its own site-packages directory

Why Do We Need Environments?

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!

Types of Python Environments:

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

2. Create and Activate a Virtual Environment

Using venv (Built-in — Recommended):

Step 1: Create the environment

# Windows
python -m venv myenv

# Mac/Linux
python3 -m venv myenv

# Create with specific Python version
py -3.11 -m venv myenv

This 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

Step 2: Activate the environment

# 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>

Step 3: Install packages inside the environment

# 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

Step 4: Install from requirements file

# Another developer clones your project and runs:
pip install -r requirements.txt

Step 5: Deactivate the environment

deactivate
# Prompt returns to normal

Step 6: Delete the environment

# Windows
rmdir /s myenv

# Mac/Linux
rm -rf myenv

Full Workflow Example:

# 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

Using Conda (for Data Science):

# 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

PyCharm — Creating Environment via IDE:

  1. Open PyCharm → New Project
  2. Under "Python Interpreter" → select "New environment using Virtualenv"
  3. Choose base interpreter (Python version)
  4. Click Create
  5. PyCharm auto-activates the venv for terminal sessions

.gitignore — Exclude venv from Git:

# .gitignore
venv/
__pycache__/
*.pyc
.env

Never commit your venv/ folder to Git! Share requirements.txt instead.


Understanding pip — Python Package Manager

# 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 check

Environment Variables with python-dotenv

pip 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}")

🎯 Mentor Questions

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 list shows packages in a human-readable format. pip freeze outputs them in package==version format suitable for requirements.txt. You use pip freeze > requirements.txt to save dependencies.

Q: What is the difference between venv and conda?

A: venv is Python's built-in tool for creating isolated Python environments. conda is 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 (or environment.yml for conda). Teammates can recreate the environment with pip install -r requirements.txt.


📝 Day 2 Quick Revision

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