Module 14 · Python Modules and Packages — topic 13 of 13
Project organization
Structuring real Python projects: a typical folder layout, separating packages, tests and scripts, a main() entry point, running with python -m, and supporting files such as README, .gitignore and requirements.
Learning objectives
- Lay out a small Python project
- Write a main() entry point
- Run a package with python -m and test its modules
Definition
Project organization is how files and folders are arranged so that code is easy to find, import, test and run. A typical project has a package folder, a tests folder, a dependency file, documentation and an entry point.
Introduction
Scripts grow into projects. A clear structure from the start means you (and others) always know where code, tests and configuration live.
Why it matters
Consistent layouts make projects easy to navigate, import correctly, test and package — and match what other Python developers expect.
Detailed explanation
A common small layout: app/ (the package), tests/ (test files), requirements.txt, README.md, .gitignore and optionally pyproject.toml. Larger projects often use a src/ folder containing the package.
Keep functions and classes in modules inside the package; keep scripts thin, calling into the package.
Use a main() function as the entry point, called under if __name__ == "__main__":. Return an exit code and pass it to SystemExit.
A package with __main__.py can be run as python -m app, which also sets up imports correctly.
Tests import the package exactly as users would (from app.core import word_count) and live in tests/.
Configuration (settings files, environment variables) is kept separate from code; secrets never go into the repository.
.gitignore excludes generated files such as .venv/, __pycache__/ and build outputs.
Syntax
- Code in a package folder.
- Tests in tests/.
- Run with python -m app.
Code examples
A typical layout
A main() entry point
Hello, Asha!
A real program would pass sys.argv; a fixed list keeps the example predictable.
More examples
Building and running a small project
app 0.1.0: 6 words | exit code 0 test passed ['app/__init__.py', 'app/__main__.py', 'app/cli.py', 'app/core.py', 'tests/test_core.py']
Step-by-step explanation
- core.py holds the logic, cli.py the user interface, and __main__.py makes the package runnable.
- python -m app runs app/__main__.py, which calls main() and exits with its return value.
- The test imports the package exactly as other code would; a test runner such as pytest would discover it automatically.
Expected output
Running “Building and running a small project” prints exactly:
Practical applications
- Open-source projects on GitHub follow this layout.
- Internal tools packaged and run with python -m.
Best practices
- Put code in a package and tests in tests/.
- Provide a main() entry point and python -m support.
- Include README, requirements and .gitignore.
Common mistakes
Everything in one script
Avoid
Prefer
Small focused modules are easier to read, test and change.
Secrets in the repository
Avoid
Prefer
Keys in code end up in version history; read them from the environment instead.
Important notes
- This completes the Modules and Packages module; next is a deeper tour of the standard library.
Key takeaways
- Organise code into a package with focused modules.
- Use main() and __main__.py as entry points.
- Keep tests, docs and configuration in their own places.
Try it yourself
- 1Restructure a single-file script into a package with core, cli and __main__ modules.
- 2Write a .gitignore suitable for a Python project with a virtual environment.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
5 questions cover the “Python Modules and Packages” module. Answers and explanations are revealed after you submit.
- ModerateWhat happens the second time a module is imported in the same program?
- ModerateCode under if __name__ == "__main__": runs when…
- ModerateWhat is printed?