Python Notes
Complete guide to setting up a professional Python development environment. Learn virtual environments, pip package manager, .env files, project structure, and best practices. Python environment setup kaise karein – Hindi mein.
Hindi: Professional Python development ke liye sirf Python install karna kaafi nahi hai. Iss guide mein seekhein virtual environments, pip, project structure, aur woh sab cheezein jo ek real developer use karta hai.
🎯 Why Environment Setup Matters
Hindi: Sochi – aap ek project karte ho jo Python 3.11 aur library X version 1.0 maangta hai, aur doosra project Python 3.13 aur library X version 3.0 maangta hai. Agar sab ek hi jagah install hoga toh clash hoga! Virtual environments isko solve karte hain.
| │ Without Virtual Environments | │ |
| │ │ ├── Project A | requests==2.28.0 │ │ |
| │ │ ├── Project B | requests==2.31.0 ← CONFLICT! │ │ |
| │ │ ├── Project C | numpy==1.21.0 │ │ |
| │ │ └── Project D | numpy==1.26.0 ← CONFLICT! │ │ |
| │ With Virtual Environments | │ |
🚫 The Problem Without Virtual Environments
Scenario:
Real-world Consequences:
- 🔴 Project A stops working after installing packages for Project B
- 🔴 Package version conflicts cause mysterious bugs
- 🔴 "Works on my machine" syndrome – works locally but not on server
- 🔴 Difficult to reproduce exact package versions for deployment
Hindi: Bina virtual environment ke, ek project ke liye install kiya package doosre project ko break kar sakta hai. Yeh professional development mein ek bada problem hai. Virtual environment is problem ko solve karta hai.
🌐 Virtual Environments (venv)
A virtual environment is an isolated Python environment where you can install packages without affecting other projects or the system Python.
Hindi: Virtual environment ek isolated sandbox hai – iske andar jo bhi package install karo woh sirf is environment tak hi limited rehta hai. Ek box mein ek project ke tools, doosre box mein doosre project ke tools.
Built-in venv Module (Recommended)
Python 3.3+ includes venv – no installation needed!
Step 1: Create a Project Folder
# Create and navigate to your project
mkdir my_python_project
cd my_python_projectStep 2: Create Virtual Environment
# Create a virtual environment named 'venv'
python -m venv venv
# Or with a custom name
python -m venv my_env
python -m venv .venv # Hidden folder (popular convention)Step 3: Activate the Virtual Environment
# Windows (Command Prompt)
venv\Scripts\activate
# Windows (PowerShell)
venv\Scripts\Activate.ps1
# macOS / Linux
source venv/bin/activateAfter activation, your prompt changes:
Hindi:(venv)prompt ke aage aana matlab virtual environment active ho gaya. Ab aap jo bhipip installkarenge woh sirf is environment mein install hoga.
Step 4: Verify the Environment
# Check which Python is being used
(venv) $ python --versionPython 3.13.0
# Check Python path (should point to venv)
(venv) $ which python # Mac/Linux
(venv) $ where python # Windows/home/user/my_python_project/venv/bin/python # Linux C:\my_python_project\venv\Scripts\python.exe # Windows
# List installed packages (should be minimal)
(venv) $ pip listPackage Version ---------- ------- pip 24.0 setuptools 69.0.2
Step 5: Install Packages in the Environment
(venv) $ pip install requests
(venv) $ pip install flask
(venv) $ pip install pandas numpy matplotlibSuccessfully installed requests-2.31.0 ... Successfully installed flask-3.0.0 ... Successfully installed pandas-2.1.3 numpy-1.26.2 matplotlib-3.8.2
Step 6: Deactivate
(venv) $ deactivateDeleting a Virtual Environment
# Simply delete the venv folder
rm -rf venv/ # Mac/Linux
rmdir /s venv # Windows Command Prompt📦 pip – Python Package Manager
pip is Python's official package installer. It downloads packages from PyPI (Python Package Index) – a repository of 500,000+ packages.
Hindi: pip Python ka package manager hai – jaise phone mein Play Store hota hai waise Python mein pip hota hai. pip install se koi bhi package PyPI se download aur install kar sakte hain.| │ Full name | pip installs packages │ |
| │ Source | PyPI (https://pypi.org) │ |
| │ Packages | 500,000+ available │ |
| │ Usage | pip install package_name │ |
Essential pip Commands:
# INSTALL PACKAGES
pip install requests # Install latest version
pip install requests==2.28.0 # Install specific version
pip install "requests>=2.25,<3" # Install version range
pip install requests flask django # Install multiple packages
# UPGRADE PACKAGES
pip install --upgrade requests # Upgrade to latest
pip install -U requests # Short form
# UNINSTALL
pip uninstall requests
pip uninstall requests flask -y # Skip confirmation (-y)
# LIST PACKAGES
pip list # All installed packages
pip list --outdated # Packages with newer versions
pip show requests # Details about a package
# SEARCH (limited in newer pip versions)
pip index versions requests # Show all available versions
# CHECK FOR DEPENDENCY CONFLICTS
pip check
# UPGRADE pip ITSELF
pip install --upgrade pip
python -m pip install --upgrade pip # Alternative
# INSTALL FROM FILE
pip install -r requirements.txt # Install from requirements file
# INSTALL IN DEVELOPMENT MODE
pip install -e . # Editable install (for your own packages)pip show – Package Details:
pip show requestsName: requests Version: 2.31.0 Summary: Python HTTP for Humans. Home-page: https://requests.readthedocs.io Author: Kenneth Reitz Author-email: me@kennethreitz.org License: Apache 2.0 Location: /home/user/venv/lib/python3.13/site-packages Requires: certifi, charset-normalizer, idna, urllib3 Required-by: flask, httpx
pip list –outdated:
pip list --outdatedPackage Version Latest Type ---------- ------- ------ ----- Pillow 9.5.0 10.1.0 wheel numpy 1.24.0 1.26.2 wheel pandas 1.5.3 2.1.3 wheel
Hindi: pip list --outdated se pata chalta hai kaunse packages ke newer versions available hain. Production mein update karne se pehle test zaroor karein.📋 Managing Dependencies with requirements.txt
When you share a project, others need to install the same packages. requirements.txt records all your dependencies.
Hindi: requirements.txt file mein project ke saare packages aur unke versions listed hote hain. Isse dusra developer ek hi command se saare packages install kar sakta hai.Generate requirements.txt:
# Freeze current environment's packages to file
(venv) $ pip freeze > requirements.txtInstall from requirements.txt:
# Create a new virtual environment first
python -m venv venv
source venv/bin/activate # Mac/Linux
venv\Scripts\activate # Windows
# Install all packages from requirements.txt
pip install -r requirements.txtCollecting certifi==2023.11.17 (from -r requirements.txt (line 1)) Collecting charset-normalizer==3.3.2 (from -r requirements.txt (line 2)) ... Successfully installed all packages!
Best Practice: Separate requirements files:
Hindi: Professional projects mein alag-alag requirements files hoti hain – ek development ke liye, ek production ke liye. Isse server par sirf zaroori packages install hote hain.
🔐 Environment Variables & .env Files
Sensitive information (passwords, API keys, database URLs) should never be hardcoded in your code. Use environment variables instead.
Hindi: Passwords, API keys, database credentials kabhi bhi directly code mein mat likhein – yeh security risk hai. Inhe.envfile mein store karein aur.gitignoremein add karein.
Why Environment Variables?
# ❌ DANGEROUS – Never do this!
DB_PASSWORD = "mypassword123"
API_KEY = "sk-abc123xyz789"
SECRET_KEY = "my_super_secret_key"
# ✅ SAFE – Use environment variables
import os
DB_PASSWORD = os.environ.get("DB_PASSWORD")
API_KEY = os.environ.get("API_KEY")
SECRET_KEY = os.environ.get("SECRET_KEY")Using python-dotenv:
# Install dotenv
pip install python-dotenvCreate .env file:
# .env file (in project root)
# Never commit this to Git!
DATABASE_URL=postgresql://user:password@localhost/mydb
SECRET_KEY=your-super-secret-key-here
API_KEY=sk-abcdef123456789
DEBUG=True
ALLOWED_HOSTS=localhost,127.0.0.1
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
EMAIL_USER=myemail@gmail.com
EMAIL_PASSWORD=myemailpasswordLoad .env in Python:
# config.py
from dotenv import load_dotenv
import os
# Load .env file
load_dotenv()
# Access environment variables
DATABASE_URL = os.getenv("DATABASE_URL")
SECRET_KEY = os.getenv("SECRET_KEY")
API_KEY = os.getenv("API_KEY")
DEBUG = os.getenv("DEBUG", "False").lower() == "true"
print(f"Database: {DATABASE_URL}")
print(f"Debug Mode: {DEBUG}")Database: postgresql://user:password@localhost/mydb Debug Mode: True
.gitignore for .env:
# .gitignore
.env
.env.local
.env.production
*.env
venv/
__pycache__/
*.pyc
.DS_StoreHindi:.envfile ko.gitignoremein daalna bahut zaroori hai – warna GitHub par push karte waqt aapka password sab dekh sakte hain! Kabhi bhi.envfile commit mat karein.
📁 Python Project Structure
Hindi: Professional Python project ki ek standard folder structure hoti hai. Yeh follow karne se code organize rehta hai aur team mein samajhna aasaan hota hai.
Simple Script Project:
Small Python Application:
Medium Flask Web App:
Python Package/Library:
Hindi:__init__.pyfile ek folder ko Python package banati hai.tests/folder mein test files rakhte hain.docs/folder mein documentation hoti hai.
pyproject.toml (Modern Package Config):
🐍 Conda Environments (Alternative)
Anaconda/Conda is an alternative environment manager, especially popular in data science.
Hindi: Conda especially data science aur machine learning projects ke liye popular hai. Isme Python packages ke alawa non-Python packages (like CUDA) bhi manage ho sakte hain.
# Install Anaconda from https://www.anaconda.com/
# Create conda environment
conda create -n myenv python=3.13
# Activate
conda activate myenv
# Install packages (conda or pip)
conda install numpy pandas matplotlib
pip install flask requests
# List environments
conda env list
# Deactivate
conda deactivate
# Remove environment
conda env remove -n myenv
# Export environment
conda env export > environment.yml
# Create from yml file
conda env create -f environment.yml# environment.yml
name: data_science_env
channels:
- defaults
- conda-forge
dependencies:
- python=3.13
- numpy=1.26.2
- pandas=2.1.3
- matplotlib=3.8.2
- jupyter=1.0.0
- scikit-learn=1.3.2
- pip:
- flask==3.0.0
- requests==2.31.0🔄 pyenv – Managing Python Versions
Hindi: pyenv se ek computer par multiple Python versions install aur switch kar sakte hain – har project ke liye alag Python version!
system 3.11.9 3.12.7 * 3.13.0 (set by /home/user/.python-version)
# Set global (default) version
pyenv global 3.13.0
# Set local version for a specific project
cd my_old_project
pyenv local 3.11.9
# Check current version
python --versionPython 3.11.9
# pyenv + venv combined (best practice):
cd my_new_project
pyenv local 3.13.0 # Set Python version for this project
python -m venv venv # Create venv with that Python
source venv/bin/activate # Activate
pip install -r requirements.txt🖥️ VS Code + Virtual Environment
Hindi: VS Code mein virtual environment ko properly configure karna zaroori hai taaki IntelliSense aur debugging sahi kaam karein.
Select venv in VS Code:
.vscode/settings.json for venv:
VS Code Terminal Auto-activates venv:
📌 Useful pip Commands Reference
Hindi: Yeh pip commands ka quick reference card hai – save karein ya print karein!
Install Most Popular Python Packages:
# Data Science
pip install numpy pandas matplotlib seaborn scikit-learn
# Web Development
pip install flask django fastapi uvicorn
# Database
pip install sqlalchemy psycopg2-binary pymongo redis
# HTTP / API
pip install requests httpx aiohttp
# Environment / Config
pip install python-dotenv pydantic
# Testing
pip install pytest pytest-cov coverage
# Code Quality
pip install black flake8 pylint isort mypy
# Utilities
pip install pillow boto3 celery📚 Complete Environment Setup – Step by Step
Hindi: Ek nayi Python project start karne ka poora process yahan ek jagah diya gaya hai.
* Serving Flask app 'main' * Debug mode: on * Running on http://127.0.0.1:5000 Press CTRL+C to quit
✅ Best Practices Checklist
🔧 Troubleshooting
❌ Error 1: venv not recognized
# Error:
$ python -m venv venv
# Error: No module named venv
# Fix (Ubuntu/Debian):
sudo apt install python3-venv
# Fix (other systems – use virtualenv instead):
pip install virtualenv
virtualenv venvHindi: Ubuntu mein python3-venv alag package hai jo install karna padta hai.❌ Error 2: Cannot activate venv on Windows PowerShell
Fix:
# Run PowerShell as Administrator and execute:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
# Then activate venv:
venv\Scripts\Activate.ps1❌ Error 3: pip install fails with SSL error
Fix:
# Temporary fix:
pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org requests
# Or use a different index:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests
# Fix SSL permanently:
pip install certifi
python -m pip install --upgrade pip certifi❌ Error 4: Module not found after installing
Cause: Package installed in wrong Python/venv.
Fix:
# Check which Python is running
which python # Mac/Linux
where python # Windows
# Make sure venv is activated (check for (venv) in prompt)
# If not activated:
source venv/bin/activate # Mac/Linux
venv\Scripts\activate # Windows
# Install in activated venv
pip install flask❌ Error 5: requirements.txt has too many packages
# Problem: pip freeze includes ALL packages (including sub-dependencies)
# Better: use pipreqs to generate only direct dependencies
pip install pipreqs
pipreqs ./ # Generate requirements.txt from imports in code❌ Error 6: Different behavior on different machines
🏗️ Complete Architecture Diagram
🚀 Summary
You now know how to set up a professional Python development environment!
Hindi: Ab aap ek professional Python developer ki tarah environment setup karna jaante hain. Virtual environment, pip, requirements.txt, .env – yeh sab production-grade development ke liye zaroori hain. Inhe apni har project mein use karein!
Next Steps:
| Step | Topic | File |
|---|---|---|
| ← Previous | First Python Program | first-python-program.mdx |
| → Next | Python Data Types (Deep Dive) | Coming Soon |
| → Advanced | Python OOP | Coming Soon |
Quick Reference Commands:
# Create & activate new project environment (copy-paste ready!)
mkdir my_project && cd my_project
python -m venv venv
source venv/bin/activate # Mac/Linux
# venv\Scripts\activate # Windows
pip install --upgrade pip
pip freeze > requirements.txt
echo "venv/" >> .gitignore
echo ".env" >> .gitignore*Last Updated: June 12, 2026 | Author: WohoTech | Category: Python Setup*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Python Environment Setup – Virtual Environments, pip & Project Structure 2026.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Python Master Course topic.
Search Terms
python-master-course, python master course, python, master, course, setup, environment, python environment setup – virtual environments, pip & project structure 2026
Related Python Master Course Topics