You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Want to learn how to create the structure to build your package? Click here.
34
+
35
+
:::
36
+
37
+
## What is the Python package source layout?
38
+
39
+
An example of the **src/package** layout structure is below.
24
40
25
41
```
26
42
myPackageRepoName
@@ -44,8 +60,8 @@ myPackageRepoName
44
60
45
61
Note the location of the following directories in the example above:
46
62
47
-
-**docs/:**discussed in our docs chapter, this directory contains your user-facing documentation website. In a **src/** layout docs/ are normally included at the same directory level of the **src/** folder.
48
-
-**tests/**this directory contains the tests for your project code. In a **src/** layout tests are normally included at the same directory level of the **src/** folder.
63
+
-**docs/:**Discussed in our docs chapter, this directory contains your user-facing documentation website. In a **src/** layout docs/ are normally included at the same directory level as the **src/** folder.
64
+
-**tests/**This directory contains the tests for your project code. In a **src/** layout, tests are normally included at the same directory level as the **src/** folder.
49
65
-**src/package/**: this is the directory that contains the code for your Python project. "Package" is normally your project's name.
50
66
51
67
Also in the above example, notice that all of the core documentation files that
@@ -61,14 +77,12 @@ include:
61
77
<!-- TODO: CHANGELOG is not mentioned in either documentation nor peer review -->
@@ -81,14 +95,13 @@ While we recommend the **src/** layout we also review the **flat** layout here.
81
95
82
96
## The src/ layout and testing
83
97
84
-
The benefit of using the **src/package** layout, particularly if you
85
-
are creating a new package, is that it ensures tests are run against the
98
+
The benefit of using the **src/package** layout is that it ensures tests are run against the
86
99
installed version of your package rather than the files in your package
87
100
working directory. If you run your tests on your files rather than the
88
-
installed version, you may be missing issues that users encounter when
101
+
installed version of your package, you may be missing issues that users encounter when
89
102
your package is installed.
90
103
91
-
If `tests/` are outside of the **src/package** directory, they aren't included in the packagewheel. This makes your package size slightly smaller which then places places a smaller storage burden on PyPI which has over 400,000 packages to support.
104
+
If `tests/` are outside the **src/package** directory, they aren't included in the package's [wheel](python-wheel). This makes your package size slightly smaller, which places a smaller storage burden on PyPI, and makes them faster to fetch.
92
105
93
106
-[Read more about reasons to use the **src/package** layout](https://hynek.me/articles/testing-packaging/)
94
107
@@ -98,14 +111,14 @@ By default, Python adds a module in your current working directory to the front
98
111
99
112
This means that if you run your tests in your package's working directory, using a flat layout, `/package/module.py`, Python will discover `package/module.py` file before it discovers the installed package.
100
113
101
-
However, if your package lives in a src/ directory structure **src/package** then it won't be, by default, added to the Python path. This means that when you import your package, Python will be forced to search the active environment (which has your package installed).
114
+
However, if your package lives in a src/ directory structure **src/package**, then it won't beadded to the Python path by default. This means that when you import your package, Python will be forced to search the active environment (which has your package installed).
102
115
103
-
Note: Python versions 3.11 and above have a path setting that can be adjusted to ensure the priority is to use installed packages first (e.g. `PYTHONSAFEPATH`).
116
+
Note: Python versions 3.11 and above have a path setting that can be adjusted to ensure the priority is to use installed packages first (e.g., `PYTHONSAFEPATH`).
104
117
```
105
118
106
-
### Sometimes tests are needed in a distribution
119
+
### Don't include tests in your package wheel
107
120
108
-
We do not recommend including tests as part of your package wheel by default. However, not including tests in your package distribution will make it harder for people other than yourself to test whether your package is functioning correctly on their system. If you have a small test suite (Python files + data), and think your users may want to run tests locally on their systems, you can include tests by moving the `tests/` directory into the **src/package** directory (see example below).
121
+
Writing [tests](tests-intro) for your package is important; however, we do not recommend including tests as part of your [package wheel](python-wheel) by default. However, not including tests in your package distribution will make it harder for people other than yourself to test whether your package runs properly on their system. If you have a small test suite (Python files + data), and think your users may want to run tests locally on their systems, you can include tests by moving the `tests/` directory into the **src/package** directory (see example below).
109
122
110
123
```bash
111
124
src/
@@ -114,69 +127,76 @@ src/
114
127
docs/
115
128
```
116
129
117
-
Including the **tests/** directory in your **src/package** directory ensures that tests will be included in your package's wheel.
130
+
Including the **tests/** directory in your **src/package** directory ensures that tests will be included in your package's [wheel](python-wheel).
118
131
119
132
Be sure to read the [pytest documentation for more about including tests in your package distribution](https://docs.pytest.org/en/7.2.x/explanation/goodpractices.html#choosing-a-test-layout-import-rules).
120
133
121
134
```{admonition} Challenges with including tests and data in a package wheel
122
135
:class: tip
123
136
124
-
Tests, especially when accompanied by test data can create a few small challenges including:
137
+
Tests, especially when accompanied by test data, can create a few small challenges, including:
125
138
126
-
- Take up space in your distribution which will build up over time as storage space on PyPI
127
-
- Large file sizes can also slow down package install.
139
+
- Take up space in your distribution, which will build up over time as storage space on PyPI
140
+
- Large file sizes can also slow down package installation.
128
141
129
-
However, in some cases, particularly in the scientific Python ecosystems you may need to include tests.
142
+
However, in some cases, particularly in the scientific Python ecosystem, you may need to include tests.
130
143
```
131
144
132
145
### **Don't include test suite datasets in your package**
133
146
134
-
If you do include your tests in your package distribution, we strongly
147
+
If you include your tests in your package distribution, we strongly
135
148
discourage you from including data in your test suite directory. Rather,
136
149
host your test data in a repository such as Figshare or Zenodo. Use a
137
150
tool such as [Pooch](https://www.fatiando.org/pooch/latest/) to access
138
151
the data when you (or a user) runs tests.
139
152
140
-
Check out the testing section of our guide for more information about tests.
153
+
For more information about Python package tests, see the [tests section of our guide](tests-intro).
141
154
142
155
- The **src/package** layout is semantically more clear. Code is always found in the
143
156
**src/package** directory, `tests/` and `docs/`are in the root directory.
144
157
145
158
```{important}
146
-
If your package tests require data, we suggest that you do NOT include that
147
-
data within your package structure. We will discuss this in more detail in a
148
-
tutorial. Include data in your package structure increases the size of your
159
+
If your package tests require data, do NOT include that
160
+
data within your package structure. Including data in your package structure increases the size of your
149
161
distribution files. This places a maintenance toll on repositories like PyPI and
150
162
Anaconda.org that have to deal with thousands of package uploads.
151
163
```
152
164
153
-
## About the flat Python package layout
154
165
155
-
Currently most scientific packages use the **flat-layout** given:
@@ -234,7 +254,7 @@ There are some benefits to the scientific community in using the flat layout.
234
254
235
255
It would be a significant maintenance cost and burden to move all of these
236
256
packages to a different layout. The potential benefits of the source layout
237
-
for these tools is not worth the maintenance investment.
257
+
for these tools are not worth the maintenance investment.
238
258
```
239
259
240
260
<!--
@@ -243,12 +263,12 @@ Not sure where to put this now ... most new users won't have multiple packages.
243
263
```{admonition} Multiple packages in a src/ folder
244
264
:class: tip
245
265
246
-
In some more advanced cases you may have more than one package in your src/ directory. See [black's GitHub repo](https://github.com/psf/black/tree/main/src) for an example of this. However, for most beginners you will likely only have one sub-directory in your **src/** folder.
266
+
In some more advanced cases, you may have more than one package in your **src/** directory. See [Black's GitHub repo](https://github.com/psf/black/tree/main/src) for an example of this. However, for most beginners you will likely only have one sub-directory in your **src/** folder.
247
267
``` -->
248
268
249
269
<!--
250
270
```{admonition} A few notes about the src/ layout
251
271
:class: tip
252
272
253
-
It is important to note here that sometimes when using the src/package structure the directory name (e.g. package name) is different from the actual project or package name. What is important to take away here is that you should store your code within a sub directory within **src/**.
273
+
It is important to note here that sometimes, when using the **src/package** structure, the directory name (e.g., package name) is different from the actual project or package name. What is important to take away here is that you should store your code within a subdirectory within **src/**.
0 commit comments