Python Development Setup
Python is a versatile programming language widely used for web development, data science, automation, and backend services. This guide will help you set up a productive Python development environment on your workstation.
Prerequisites
Before setting up Python, ensure you have:
- A code editor or IDE (VS Code, PyCharm, or similar)
- Terminal/command line access
- Git installed and configured
Installation Options
Option 1: Using pyenv (Recommended)
pyenv allows you to manage multiple Python versions easily:
# Install pyenv (macOS with Homebrew)
brew install pyenv
# Install pyenv (Linux)
curl https://pyenv.run | bash
# Add to your shell profile (~/.zshrc or ~/.bashrc)
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
# Install latest Python version
pyenv install 3.12.0
pyenv global 3.12.0
Option 2: System Installation
Download Python from python.org or use your system package manager:
# macOS with Homebrew
brew install python
# Ubuntu/Debian
sudo apt update && sudo apt install python3 python3-pip
# Windows (using Chocolatey)
choco install python
Essential Tools
Package Management
# Upgrade pip
python -m pip install --upgrade pip
# Install pipenv for dependency management
pip install pipenv
# Or use poetry (alternative)
pip install poetry
Virtual Environments
# Create a virtual environment
python -m venv myproject-env
# Activate (macOS/Linux)
source myproject-env/bin/activate
# Activate (Windows)
myproject-env\Scripts\activate
# Deactivate
deactivate
Development Environment
VS Code Extensions
Install these essential Python extensions:
- Python (Microsoft)
- Pylance (Microsoft)
- Python Docstring Generator
- autoDocstring
- Black Formatter
Code Quality Tools
# Install development tools
pip install black flake8 pylint mypy pytest
# Format code with Black
black your_file.py
# Lint with flake8
flake8 your_file.py
# Type checking with mypy
mypy your_file.py
# Run tests with pytest
pytest
Project Structure
Create a standard Python project structure:
my-python-project/
├── src/
│ └── myproject/
│ ├── __init__.py
│ └── main.py
├── tests/
│ ├── __init__.py
│ └── test_main.py
├── requirements.txt
├── setup.py
├── README.md
└── .gitignore
Common Workflows
Starting a New Project
# Create project directory
mkdir my-project && cd my-project
# Initialize virtual environment
python -m venv venv
source venv/bin/activate # or venv\Scripts\activate on Windows
# Install project dependencies
pip install -r requirements.txt
# Install development dependencies
pip install -r requirements-dev.txt
Dependency Management
# Generate requirements.txt
pip freeze > requirements.txt
# Install from requirements
pip install -r requirements.txt
# Using pipenv
pipenv install requests
pipenv install pytest --dev
Best Practices
- Always use virtual environments for project isolation
- Pin dependency versions in requirements.txt
- Use type hints for better code documentation
- Follow PEP 8 style guidelines
- Write tests for your code
- Use meaningful variable and function names
Troubleshooting
Common Issues
Python command not found:
# Add Python to PATH or use python3
export PATH="/usr/local/bin/python3:$PATH"
Permission errors with pip:
# Use --user flag
pip install --user package_name
SSL certificate errors:
# Upgrade certificates
pip install --upgrade certifi
Next Steps
- Set up your first Python project
- Explore web frameworks like Django or FastAPI
- Learn about testing with pytest
- Check out our Backend Development guides for Python APIs