Create Python Project Script

A simple shell script to scaffold new Python project directories with best practices baked in: virtual environment setup, common folder layout, and initial `.gitignore`.

📦 What It Does

This script:

  • Creates a new Python project folder structure
  • Sets up a virtual environment using pyenv
  • Adds a .gitignore file with sensible defaults
  • Initializes a Git repo
  • Creates common directories:
    • src/ – Source code
    • tests/ – Test suite
    • data/ – Project data (excluded from Git)
    • config/ – For config files like .env, prod.env, etc.
    • notebooks/ – Jupyter notebooks, if needed
    • docs/ – Documentation (e.g. Markdown files, diagrams)

🛠 Prerequisites

  • pyenv
  • python

How to use

🛠 Usage

create_py_project.sh my-awesome-project 3.12.10

Technical notes

#!/bin/bash

set -e

if [ -z "$1" ]; then
  echo "Usage: create_py_project.sh <project-name>"
  exit 1
fi

PROJECT_NAME="$1"
PYTHON_VERSION="${2:-3.11.9}"
PROJECT_ROOT="$HOME/Projects/$PROJECT_NAME"

# Create project structure
mkdir -p "$PROJECT_ROOT"/{src/"$PROJECT_NAME",tests,data,notebooks,scripts,configs,logs,docs}

# Add placeholder README.md to ignored folders
echo "# Data folder – not tracked by Git" > "$PROJECT_ROOT/data/README.md"
echo "# Logs folder – not tracked by Git" > "$PROJECT_ROOT/logs/README.md"

# Create .gitignore
cat > "$PROJECT_ROOT/.gitignore" <<EOF
# Python cache and artifacts
__pycache__/
*.py[cod]
*.so

# Virtual environments
.venv/
env/
venv/

# Local environment/configs
.env
*.env

# IDE/project settings
.vscode/
.idea/

# OS files
.DS_Store

# Logs and data
logs/
data/
EOF

# Create README.md
cat > "$PROJECT_ROOT/README.md" <<EOF
# $PROJECT_NAME

Project scaffold created by \`create_py_project.sh\`.
EOF

# Create requirements.txt (empty for now)
touch "$PROJECT_ROOT/requirements.txt"

# Set Python version for pyenv
echo "$PYTHON_VERSION" > "$PROJECT_ROOT/.python-version"

# Initialize Git repo
cd "$PROJECT_ROOT"
git init -b main
git add .
git commit -m "Initial project structure for $PROJECT_NAME"

# Create virtual environment
pyenv install -s "$PYTHON_VERSION"
pyenv local "$PYTHON_VERSION"
"$HOME/.pyenv/versions/$PYTHON_VERSION/bin/python" -m venv venv

echo "✅ Project '$PROJECT_NAME' created at $PROJECT_ROOT"
echo "📦 Python version: $PYTHON_VERSION"
echo "🐍 Virtualenv: $PROJECT_NAME-venv"