Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Standard Project Layout | Python Project Structure & Packaging Basics
Python Packaging and Distribution

bookStandard Project Layout

When creating a Python project for sharing or distribution, using a standard directory structure makes the code easier to understand, maintain, and package. It separates source code, tests, documentation, and configuration so each part is easy to manage.

A common and recommended layout places the main package inside a src/ directory to prevent import issues. Projects typically also include a tests/ folder, a README.md, a LICENSE, and configuration files like pyproject.toml or setup.cfg for packaging and build settings.

# my_package_project/
# β”œβ”€β”€ src/
# β”‚   └── my_package/
# β”‚       β”œβ”€β”€ __init__.py
# β”‚       └── core.py
# β”œβ”€β”€ tests/
# β”‚   └── test_core.py
# β”œβ”€β”€ README.md
# β”œβ”€β”€ LICENSE
# └── pyproject.toml

In this structure, the src/ directory holds your actual package code inside my_package/. The __init__.py file marks it as a package, and files like core.py contain the main logic. The tests/ folder includes test files such as test_core.py. The README.md explains usage, the LICENSE defines usage terms, and pyproject.toml stores build and package metadata.

This layout prevents common issues like wrong imports or extra files in the distribution and follows Python packaging standards, making the project easier to test, share, and maintain.

Note
Note

Using a src/ layout is considered best practice for modern Python packages and helps catch import-related errors early.

question mark

In a standard Python project layout, where is the main package source code typically placed?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain more about what goes into the pyproject.toml file?

What are some best practices for writing tests in the tests/ folder?

How do I publish my package once I have this structure set up?

bookStandard Project Layout

Swipe to show menu

When creating a Python project for sharing or distribution, using a standard directory structure makes the code easier to understand, maintain, and package. It separates source code, tests, documentation, and configuration so each part is easy to manage.

A common and recommended layout places the main package inside a src/ directory to prevent import issues. Projects typically also include a tests/ folder, a README.md, a LICENSE, and configuration files like pyproject.toml or setup.cfg for packaging and build settings.

# my_package_project/
# β”œβ”€β”€ src/
# β”‚   └── my_package/
# β”‚       β”œβ”€β”€ __init__.py
# β”‚       └── core.py
# β”œβ”€β”€ tests/
# β”‚   └── test_core.py
# β”œβ”€β”€ README.md
# β”œβ”€β”€ LICENSE
# └── pyproject.toml

In this structure, the src/ directory holds your actual package code inside my_package/. The __init__.py file marks it as a package, and files like core.py contain the main logic. The tests/ folder includes test files such as test_core.py. The README.md explains usage, the LICENSE defines usage terms, and pyproject.toml stores build and package metadata.

This layout prevents common issues like wrong imports or extra files in the distribution and follows Python packaging standards, making the project easier to test, share, and maintain.

Note
Note

Using a src/ layout is considered best practice for modern Python packages and helps catch import-related errors early.

question mark

In a standard Python project layout, where is the main package source code typically placed?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 3
some-alt