Python - Project Structure
Directory Structure
Basic project structure, as suggested by Kenneth Reitz’s Github repo:
README.rst
LICENSE
setup.py
requirements.txt
sample/__init__.py
sample/core.py
sample/helpers.py
docs/conf.py
docs/index.rst
tests/test_basic.py
tests/test_advanced.py
Tip
Don’t know which license to use? Try choosealicense.com!
Making Tests Work
This PyTest article will answer all of your questions. Trust me.
The below method is outdated and (probably) should be ignored.
Copied from https://docs.python-guide.org/writing/structure/
To give individual tests import context (so they can access your module), create a tests/context.py file:
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import mymodule
Then, within the individual test modules, import the module like so:
from .context import mymodule
This will always work as expected, regardless of installation method.
Larger Projects
Copied from https://realpython.com/python-application-layouts/
A larger project structure with submodules and data files might look something like:
helloworld/
│
├── bin/
│
├── docs/
│ ├── hello.md
│ └── world.md
│
├── helloworld/
│ ├── __init__.py
│ ├── runner.py
│ ├── hello/
│ │ ├── __init__.py
│ │ ├── hello.py
│ │ └── helpers.py
│ │
│ └── world/
│ ├── __init__.py
│ ├── helpers.py
│ └── world.py
│
├── data/
│ ├── input.csv
│ └── output.xlsx
│
├── tests/
│ ├── hello
│ │ ├── helpers_tests.py
│ │ └── hello_tests.py
│ │
│ └── world/
│ ├── helpers_tests.py
│ └── world_tests.py
│
├── .gitignore
├── LICENSE
└── README.md