Skip to content

Guide to Starting a Standard Python Project

This guide will walk you through the process of creating a well-structured Python project from scratch.

1. Create Project Structure

Start by creating a directory structure for your project:

bash
# Create main project directory
mkdir my_project
cd my_project

# Create standard directories
mkdir -p my_project/tests docs

A typical Python project structure looks like:

my_project/
├── my_project/       # Source code package
│   ├── __init__.py   # Makes the directory a package
│   ├── core.py       # Core functionality
│   └── helpers.py    # Helper functions
├── tests/            # Test directory
│   ├── __init__.py
│   └── test_core.py
├── docs/             # Documentation
├── README.md         # Project description
├── requirements.txt  # Dependencies
├── setup.py          # Package and distribution management
└── .gitignore        # Specifies intentionally untracked files

2. Set Up a Virtual Environment

Creating a virtual environment isolates your project dependencies:

bash
# Create virtual environment
python -m venv venv

# Activate the environment
# On Linux/macOS:
source venv/bin/activate
# On Windows:
# venv\Scripts\activate

3. Create Essential Files

Create a basic README.md

markdown
# My Project

Brief description of your project

## Installation

`pip install my-project`

## Usage

```python
from my_project import core

core.main()
```

Contributing

Instructions for contributors

License

This project is licensed under the MIT License


### Create setup.py

```python
from setuptools import setup, find_packages

setup(
    name="my-project",
    version="0.1.0",
    packages=find_packages(include=["my_project", "my_project.*"]),
    install_requires=[
        # List your dependencies here
    ],
    author="Your Name",
    author_email="your.email@example.com",
    description="A short description of your project",
    keywords="your,keywords,here",
    url="https://github.com/yourusername/my-project",
    classifiers=[
        "Development Status :: 3 - Alpha",
        "Intended Audience :: Developers",
        "License :: OSI Approved :: MIT License",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.8",
    ],
    python_requires=">=3.6",
)

Create requirements.txt

text
# Dependencies
pytest>=7.0.0
black>=22.3.0
flake8>=4.0.1

Create init.py in your package directory

python
"""My Project package."""

__version__ = "0.1.0"

4. Initialize Git Repository

bash
# Initialize git
git init

# Create .gitignore
cat > .gitignore << EOL
# Byte-compiled files
__pycache__/
*.py[cod]
*$py.class

# Distribution / packaging
dist/
build/
*.egg-info/

# Virtual environments
venv/
env/
ENV/

# Testing
.coverage
htmlcov/

# IDE specific files
.idea/
.vscode/
*.swp
EOL

# Initial commit
git add .
git commit -m "Initial project structure"

5. Create Basic Tests

Create tests/test_core.py:

python
"""Tests for core functionality."""

import pytest
from my_project import core

def test_placeholder():
    """Placeholder test function."""
    assert True

6. Install Development Dependencies

bash
# Install dev dependencies
pip install -e .
pip install -r requirements.txt

7. Start Coding

Create my_project/core.py:

python
"""Core functionality of my_project."""

def main():
    """Entry point for the application."""
    print("Hello, world!")

if __name__ == "__main__":
    main()

8. Run Tests

bash
pytest

9. Use a Code Formatter and Linter

bash
# Format your code
black my_project tests

# Lint your code
flake8 my_project tests

Next Steps

  • Add more detailed documentation
  • Set up continuous integration
  • Publish your package to PyPI
  • Create a development workflow

This guide provides the foundation for a well-structured Python project that follows best practices!