Python Tools β
π§ Development Environment β
Python Installation and Management β
bash
# Check Python version
python --version
python3 --version
# Install Python (macOS with Homebrew)
brew install python
# Install Python (Ubuntu/Debian)
sudo apt update
sudo apt install python3 python3-pip
# Install Python (Windows)
# Download from python.org or use Microsoft StoreVirtual Environments β
bash
# Create virtual environment
python -m venv myenv
# Activate virtual environment
# On macOS/Linux:
source myenv/bin/activate
# On Windows:
myenv\Scripts\activate
# Deactivate virtual environment
deactivate
# Using conda
conda create --name myenv python=3.9
conda activate myenv
conda deactivatePackage Management with pip β
bash
# Install packages
pip install requests
pip install numpy pandas matplotlib
# Install specific version
pip install Django==3.2.0
# Install from requirements file
pip install -r requirements.txt
# List installed packages
pip list
pip freeze
# Create requirements file
pip freeze > requirements.txt
# Upgrade packages
pip install --upgrade package_name
pip install --upgrade pip
# Uninstall packages
pip uninstall package_nameπ Code Editors and IDEs β
Popular Python IDEs β
python
# VS Code - Lightweight, extensible
# - Python extension by Microsoft
# - Jupyter notebook support
# - Integrated terminal and debugging
# PyCharm - Full-featured IDE
# - Professional and Community editions
# - Excellent debugging and refactoring
# - Built-in version control
# Jupyter Notebook/Lab - Interactive development
# - Great for data science and prototyping
# - Supports markdown and visualizations
# - Can export to various formatsEssential VS Code Extensions β
json
{
"recommendations": [
"ms-python.python",
"ms-python.flake8",
"ms-python.black-formatter",
"ms-toolsai.jupyter",
"ms-python.pylint",
"ms-python.mypy-type-checker"
]
}π§ͺ Testing Tools β
unittest (Built-in) β
python
import unittest
class TestMathOperations(unittest.TestCase):
def test_addition(self):
self.assertEqual(2 + 2, 4)
def test_division(self):
self.assertEqual(10 / 2, 5)
def test_division_by_zero(self):
with self.assertRaises(ZeroDivisionError):
10 / 0
if __name__ == '__main__':
unittest.main()pytest (Popular Third-party) β
python
# Install: pip install pytest
def add(a, b):
return a + b
def test_add():
assert add(2, 3) == 5
assert add(-1, 1) == 0
assert add(0, 0) == 0
def test_add_strings():
assert add("hello", "world") == "helloworld"
# Run tests: pytest test_file.pyTest Coverage β
bash
# Install coverage
pip install coverage
# Run tests with coverage
coverage run -m pytest
coverage report
coverage html # Generate HTML reportπ¨ Code Formatting and Linting β
Black - Code Formatter β
bash
# Install black
pip install black
# Format a file
black my_script.py
# Format entire directory
black .
# Check without formatting
black --check .
# Configuration in pyproject.toml
[tool.black]
line-length = 88
target-version = ['py39']Flake8 - Linter β
bash
# Install flake8
pip install flake8
# Lint a file
flake8 my_script.py
# Configuration in .flake8
[flake8]
max-line-length = 88
exclude = .git,__pycache__,venv
ignore = E203,W503isort - Import Sorting β
bash
# Install isort
pip install isort
# Sort imports in a file
isort my_script.py
# Configuration in pyproject.toml
[tool.isort]
profile = "black"
multi_line_output = 3π Debugging Tools β
Built-in Debugger (pdb) β
python
import pdb
def problematic_function(x, y):
pdb.set_trace() # Breakpoint
result = x / y
return result
# Debug commands:
# n - next line
# s - step into
# c - continue
# l - list code
# p variable_name - print variable
# q - quitIPython Debugger β
python
# Install: pip install ipython
# In code:
from IPython import embed
embed() # Drop into IPython shell
# Or use magic command in IPython/Jupyter:
%pdb on # Enable automatic debugging on exceptionsLogging for Debugging β
python
import logging
# Configure logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
def my_function(x, y):
logger.debug(f"Input values: x={x}, y={y}")
result = x * y
logger.info(f"Calculation result: {result}")
return result
# Usage
result = my_function(5, 3)π Performance Profiling β
cProfile β
python
import cProfile
def slow_function():
total = 0
for i in range(1000000):
total += i ** 2
return total
# Profile a function
cProfile.run('slow_function()')
# Profile a script
# python -m cProfile my_script.pyline_profiler β
bash
# Install
pip install line_profiler
# Use @profile decorator
@profile
def slow_function():
total = 0
for i in range(1000000):
total += i ** 2
return total
# Run profiler
kernprof -l -v my_script.pyMemory Profiling β
bash
# Install memory_profiler
pip install memory_profiler
# Use @profile decorator
@profile
def memory_intensive_function():
big_list = [i for i in range(1000000)]
return sum(big_list)
# Run memory profiler
python -m memory_profiler my_script.pyπ¦ Package Distribution β
Creating a Package β
my_package/
βββ setup.py
βββ README.md
βββ requirements.txt
βββ my_package/
β βββ __init__.py
β βββ module1.py
β βββ module2.py
βββ tests/
βββ __init__.py
βββ test_module1.pysetup.py β
python
from setuptools import setup, find_packages
setup(
name="my_package",
version="0.1.0",
author="Your Name",
author_email="your.email@example.com",
description="A short description of the package",
long_description=open("README.md").read(),
long_description_content_type="text/markdown",
url="https://github.com/yourusername/my_package",
packages=find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires=">=3.6",
install_requires=[
"requests>=2.25.0",
"numpy>=1.20.0",
],
)Building and Publishing β
bash
# Install build tools
pip install build twine
# Build package
python -m build
# Upload to TestPyPI
twine upload --repository testpypi dist/*
# Upload to PyPI
twine upload dist/*π Popular Python Libraries β
Web Development β
python
# Flask - Lightweight web framework
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/api/users')
def get_users():
return jsonify({'users': ['Alice', 'Bob']})
# Django - Full-featured web framework
# FastAPI - Modern, fast API framework
# Requests - HTTP libraryData Science β
python
# NumPy - Numerical computing
import numpy as np
array = np.array([1, 2, 3, 4, 5])
# Pandas - Data manipulation
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# Matplotlib - Plotting
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
# Scikit-learn - Machine learning
from sklearn.linear_model import LinearRegressionGUI Development β
python
# Tkinter - Built-in GUI toolkit
import tkinter as tk
root = tk.Tk()
root.title("My App")
label = tk.Label(root, text="Hello, World!")
label.pack()
root.mainloop()
# PyQt/PySide - Advanced GUI
# Kivy - Cross-platform GUIπ Version Control Integration β
Git Integration β
bash
# Initialize git repository
git init
# Create .gitignore for Python
echo "__pycache__/
*.pyc
*.pyo
*.pyd
.Python
venv/
.venv/
.env
.DS_Store" > .gitignore
# Commit changes
git add .
git commit -m "Initial commit"Pre-commit Hooks β
yaml
# .pre-commit-config.yaml
repos:
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 4.0.1
hooks:
- id: flake8
- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isortπ³ Docker Integration β
Dockerfile for Python β
dockerfile
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]Docker Compose β
yaml
version: '3.8'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/app
environment:
- FLASK_ENV=developmentπ― Best Practices β
Project Structure β
my_project/
βββ README.md
βββ requirements.txt
βββ setup.py
βββ .gitignore
βββ .env.example
βββ src/
β βββ my_project/
β βββ __init__.py
β βββ main.py
β βββ utils.py
βββ tests/
β βββ __init__.py
β βββ test_main.py
βββ docs/
β βββ index.md
βββ scripts/
βββ deploy.shConfiguration Management β
python
# config.py
import os
from dataclasses import dataclass
@dataclass
class Config:
debug: bool = os.getenv('DEBUG', 'False').lower() == 'true'
database_url: str = os.getenv('DATABASE_URL', 'sqlite:///app.db')
secret_key: str = os.getenv('SECRET_KEY', 'dev-secret-key')
@classmethod
def from_env(cls):
return cls()
# Usage
config = Config.from_env()π― Key Takeaways β
- Virtual environments isolate project dependencies
- pip is the standard package manager for Python
- Testing is essential - use unittest or pytest
- Code formatting tools like Black improve code quality
- Linting with flake8 catches common errors
- Debugging tools help identify and fix issues
- Profiling helps optimize performance bottlenecks
- Version control with Git is essential for collaboration
- Docker enables consistent deployment environments
- Good project structure makes code maintainable
- Configuration management keeps settings organized
This completes the Python Fundamentals section! Continue to: NLP Fundamentals β