Python Installation

Ubuntu/Debian

1
2
3
4
5
6
7
# Install Python
sudo apt update
sudo apt install python3
sudo apt install python3-pip

# Install virtual environment
sudo apt install python3-venv

macOS

1
2
# Using Homebrew
brew install python3

Windows

Download from Python.org or use:

1
winget install Python.3

Virtual Environments

Using venv (Built-in)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Create virtual environment
python3 -m venv myenv

# Activate virtual environment
source myenv/bin/activate      # Linux/macOS
myenv\Scripts\activate.bat     # Windows CMD
myenv\Scripts\Activate.ps1     # Windows PowerShell

# Deactivate
deactivate

Package Management with pip

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Install packages
pip install package_name
pip install package_name==1.2.3  # Specific version

# Install from requirements.txt
pip install -r requirements.txt

# Generate requirements.txt
pip freeze > requirements.txt

# Upgrade pip
python -m pip install --upgrade pip

# List installed packages
pip list

Anaconda Environment

Installation

Conda Commands

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Create environment
conda create --name myenv python=3.9

# Activate environment
conda activate myenv

# Deactivate environment
conda deactivate

# List environments
conda env list

# Install packages
conda install package_name

# Update conda
conda update conda

# List installed packages
conda list

# Remove environment
conda env remove --name myenv

Environment Management

1
2
3
4
5
6
7
8
# Export environment
conda env export > environment.yml

# Create environment from file
conda env create -f environment.yml

# Clean unused packages and caches
conda clean --all

Project Structure Best Practices

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
project_name/
├── README.md
├── requirements.txt
├── setup.py
├── .gitignore
├── .env
├── docs/
├── tests/
│   ├── __init__.py
│   └── test_main.py
└── project_name/
    ├── __init__.py
    ├── main.py
    └── utils/
        └── __init__.py

Development Tools

Code Formatting

1
2
3
4
5
6
7
# Install formatters
pip install black
pip install isort

# Format code
black .
isort .

Linting

1
2
3
4
5
6
7
# Install linters
pip install flake8
pip install pylint

# Run linters
flake8 .
pylint your_module.py

Type Checking

1
2
3
4
5
# Install mypy
pip install mypy

# Run type checking
mypy your_module.py

Testing

1
2
3
4
5
6
7
# Install pytest
pip install pytest
pip install pytest-cov

# Run tests
pytest
pytest --cov=your_module  # With coverage

Common Development Patterns

Environment Variables

1
2
3
4
5
6
# Using python-dotenv
from dotenv import load_dotenv
import os

load_dotenv()
api_key = os.getenv('API_KEY')

Logging

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import logging

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)

logger = logging.getLogger(__name__)
logger.info('This is an info message')

Exception Handling

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
try:
    # Risky operation
    result = some_function()
except ValueError as e:
    logger.error(f"Value error occurred: {e}")
except Exception as e:
    logger.error(f"Unexpected error: {e}")
else:
    # Run if no exception
    process_result(result)
finally:
    # Always run
    cleanup()

Performance Tips

  1. Use list comprehensions instead of loops when possible
  2. Use generators for large datasets
  3. Profile code with cProfile or line_profiler
  4. Use appropriate data structures (sets for lookups, etc.)
  5. Consider using NumPy for numerical operations
  6. Use multiprocessing for CPU-bound tasks
  7. Use asyncio for I/O-bound tasks

Security Best Practices

  1. Never store secrets in code
  2. Use environment variables for sensitive data
  3. Keep dependencies updated
  4. Validate all input data
  5. Use HTTPS for network connections
  6. Follow principle of least privilege
  7. Regularly update Python and packages