Skip to content

Commit 537d055

Browse files
authored
Add initial draft of src vs flat layout discussion (#1150)
1 parent 41eb747 commit 537d055

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

source/discussions/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ specific topic. If you're just trying to get stuff done, see
1212
pip-vs-easy-install
1313
install-requires-vs-requirements
1414
wheel-vs-egg
15+
src-layout-vs-flat-layout
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
=========================
2+
src layout vs flat layout
3+
=========================
4+
5+
The "flat layout" refers to organising a project's files in a folder or
6+
repository, such that the various configuration files and
7+
:term:`import packages <Import Package>` are all in the top-level directory.
8+
9+
::
10+
11+
.
12+
├── README.md
13+
├── noxfile.py
14+
├── pyproject.toml
15+
├── setup.py
16+
├── awesome_package/
17+
│ ├── __init__.py
18+
│ └── module.py
19+
└── tools/
20+
├── generate_awesomeness.py
21+
└── decrease_world_suck.py
22+
23+
The "src layout" deviates from the flat layout by moving the code that is
24+
intended to be importable (i.e. ``import awesome_package``, also known as
25+
:term:`import packages <Import Package>`) into a subdirectory. This
26+
subdirectory is typically named ``src/``, hence "src layout".
27+
28+
::
29+
30+
.
31+
├── README.md
32+
├── noxfile.py
33+
├── pyproject.toml
34+
├── setup.py
35+
├── src/
36+
│ └── awesome_package/
37+
│ ├── __init__.py
38+
│ └── module.py
39+
└── tools/
40+
├── generate_awesomeness.py
41+
└── decrease_world_suck.py
42+
43+
Here's a breakdown of the important behaviour differences between the src
44+
layout and the flat layout:
45+
46+
* The src layout requires installation of the project to be able to run its
47+
code, and the flat layout does not.
48+
49+
This means that the src layout involves an additional step in the
50+
development workflow of a project (typically, an
51+
:doc:`editable installation <setuptools:userguide/development_mode>`
52+
is used for development and a regular installation is used for testing).
53+
54+
* The src layout helps prevent accidental usage of the in-development copy of
55+
the code.
56+
57+
This is relevant since the Python interpreter includes the current working
58+
directory as the first item on the import path. This means that if an import
59+
package exists in the current working directory with the same name as an
60+
installed import package, the variant from the current working directory will
61+
be used. This can lead to subtle misconfiguration of the project's packaging
62+
tooling, which could result in files not being included in a distribution.
63+
64+
The src layout helps avoid this by keeping import packages in a directory
65+
separate from the root directory of the project, ensuring that the installed
66+
copy is used.
67+
68+
* The src layout helps enforce that an
69+
:doc:`editable installation <setuptools:userguide/development_mode>` is only
70+
able to import files that were meant to be importable.
71+
72+
This is especially relevant when the editable installation is implemented
73+
using a `path configuration file <https://docs.python.org/3/library/site.html#index-2>`_
74+
that adds the directory to the import path.
75+
76+
The flat layout would add the other project files (eg: ``README.md``,
77+
``tox.ini``) and packaging/tooling configuration files (eg: ``setup.py``,
78+
``noxfile.py``) on the import path. This would make certain imports work
79+
in editable installations but not regular installations.

0 commit comments

Comments
 (0)