Skip to main content

🐍 Setting Up Python Workspace

Introduction

A well-organized Python workspace is essential for productive development. This guide covers setting up virtual environments, package management with pip, and project structure best practices. You'll learn how to create isolated environments, manage dependencies, and organize your Python projects professionally.

1. Install Python

macOS (using Homebrew)

# Install Homebrew if you don't have it
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# Install Python
brew install python

Ubuntu/Debian

sudo apt update
sudo apt install python3 python3-pip python3-venv

Windows

Download from python.org or use the Microsoft Store.

Verify Installation

python3 --version
pip3 --version

2. Set Up Virtual Environments

Virtual environments isolate your project dependencies and prevent conflicts between different projects.

Create a Virtual Environment

# Navigate to your project directory
cd ~/my-python-project

# Create virtual environment
python3 -m venv venv

# Activate virtual environment
# On macOS/Linux:
source venv/bin/activate

# On Windows:
# venv\Scripts\activate

Deactivate Virtual Environment

deactivate

3. Package Management with pip

Upgrade pip

pip install --upgrade pip

Install Packages

# Install a single package
pip install requests

# Install multiple packages
pip install requests numpy pandas matplotlib

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

Create requirements.txt

# Generate requirements file
pip freeze > requirements.txt

# Or manually create it
echo "requests==2.31.0" > requirements.txt
echo "numpy==1.24.3" >> requirements.txt
echo "pandas==2.0.3" >> requirements.txt

4. Project Structure Best Practices

Create a well-organized project structure:

my-python-project/
├── venv/ # Virtual environment (don't commit)
├── src/ # Source code
│ ├── __init__.py
│ ├── main.py
│ └── utils/
│ ├── __init__.py
│ └── helpers.py
├── tests/ # Test files
│ ├── __init__.py
│ └── test_main.py
├── data/ # Data files
├── docs/ # Documentation
├── .gitignore # Git ignore file
├── requirements.txt # Dependencies
├── README.md # Project documentation
└── setup.py # Package setup (optional)