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
Copy file name to clipboardExpand all lines: index.md
-32Lines changed: 0 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -128,35 +128,3 @@ Good meets the requirements. Going beyond the minimum can make package maintenan
128
128
129
129
This guide is now a work in progress. If you have ideas of things you'd like
130
130
to see here, [we invite you to open an issue on GitHub that details any changes or additions that you'd like to see.](https://github.com/pyOpenSci/python-package-guide/issues).
131
-
132
-
<!--
133
-
COMMENTED OUT TEXT TO BE MOVED
134
-
135
-
136
-
# TODO LINK TO CI BUILDS FOR Documentation>
137
-
Maybe we can curate a list of CI builds that people can use??? or is that moving too close to a cookie cutter situation
138
-
139
-
The text below is being moved to the packaging infrastructure section which
140
-
doesn't exist YET... but will soon .
141
-
pyOpenSci packages must:
142
-
143
-
- Contain full documentation for any user-facing functions.
144
-
- Have a test suite that covers the major functionality of the package.
145
-
- Use continuous integration.
146
-
- Use an OSI approved software license.
147
-
148
-
149
-
## Other recommendations
150
-
### Python version support
151
-
You should always be explicit about which versions of Python your package supports.
152
-
Keeping compatibility with old Python versions can be difficult as functionality changes.
153
-
A good rule of thumb is that the package should support, at least,
154
-
the latest three Python versions (e.g., 3.8, 3.7, 3.6).
155
-
156
-
### Code Style
157
-
pyOpenSci encourages authors to consult [PEP 8](https://www.python.org/dev/peps/pep-0008/) for information on how to style your code.
158
-
159
-
### Linting
160
-
An automatic linter (e.g. flake8) can help ensure your code is clean and free of syntax errors. These can be integrated with your CI.
Copy file name to clipboardExpand all lines: package-structure-code/intro.md
-15Lines changed: 0 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -61,21 +61,6 @@ package to be reviewed and accepted into our pyOpenSci open source ecosystem.
61
61
Please check out our [package scope page](https://www.pyopensci.org/software-peer-review/about/package-scope.html) and [review requirements in our author guide](https://www.pyopensci.org/software-peer-review/how-to/author-guide.html#) if you are looking for pyOpenSci's Python package review requirements!
62
62
```
63
63
64
-
<!--
65
-
```{tip}
66
-
### Python packaging resources that we love
67
-
68
-
We think the resources below are excellent but each have particular opinions
69
-
that you may or may not find in our packaging guide. For instance, the PyPA
70
-
guide encourages users to store their package in a `src/package-name` directory.
71
-
While we accept that approach many of our community members prefer to not use
72
-
the `src` directory.
73
-
74
-
* [Python packaging for research software engineers](https://merely-useful.tech/py-rse/)
Copy file name to clipboardExpand all lines: package-structure-code/python-package-build-tools.md
+7-8Lines changed: 7 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -168,17 +168,16 @@ questions:
168
168
- Flit, Hatch, PDM or Poetry (read below for more)
169
169
170
170
1.**Does your tool have a few C or C++ extensions?** Great, we suggest using
171
-
**PDM** for the time being. It is the only tool in the list below that has documented
172
-
workflow to support such extensions. It also supports other back-ends such as scikit-build and meson-python that will allow you to fully customize your build.
171
+
**PDM** for the time being. It is the only tool in the list below that has both documented
172
+
workflow to support such extensions and support for other back-ends in the case that build hooks are not enough for your workflow. PDM supports other back-ends such as scikit-build and meson-python that will allow you to fully customize your package's build.
173
173
174
-
NOTE: You can also use Hatch for non pure python builds but you will need to
175
-
write your own plugin for this support.
174
+
NOTE: You can also use Hatch for non pure python builds. Hatch, similar to PDM, allows you to write your own build hooks or plugins to support custom build steps. But currently, hatch does not support other build back ends. Many of the core scientific packages are moving to meson-python to build their packages. Thus, we appreciate that PDM can work with meson-python specifically.
176
175
177
176
## Python packaging tools summary
178
177
179
-
<!-- NOTE - add language around the front end means that you have less individual tools in your build - such as nox / make with hatch -->
178
+
Below, we summarize features offered by the most popular build front end tools. It is important to keep in mind that these
179
+
front-end tools remove the need to use other core tools in your workflow. For example if you use setuptools, you will need to also use Build and Twine to build your package and publish to PyPI. But if you use Poetry, Hatch or PDM you can do all of those things using the same tool (e.g. `hatch build`, `hatch publish` or `pdm build`, `pdm publish`).
180
180
181
-
Below, we summarize features offered by the most popular build front end tools.
182
181
Note that because setuptools does not offer a front-end interface, it is not
183
182
included in the table.
184
183
@@ -313,7 +312,7 @@ using a tool like **Make** or **Nox**.
313
312
314
313
Use Other Build Backends|✖| Switching out build back-ends is not currently an option when using Hatch. However this feature is coming to the package in the near future.
315
314
Dependency management|✅|Hatch can help you add dependencies to your **pyproject.toml** metadata.
316
-
Select your environment manager of choice (conda, venv, etc)|✅ | Hatch does allow you to select the (pip) environment that you want to use for managing and building your package. However if you want to use conda [you will need to use a plugin](https://github.com/OldGrumpyViking/hatch-conda).
315
+
Select your environment manager of choice (conda, venv, etc)|✅ | Hatch does allow you to select the (pip) environment that you want to use for managing and building your package. However if you want to use Conda [you will need to use a plugin](https://github.com/OldGrumpyViking/hatch-conda).
317
316
Publish to PyPI and test PyPI|✅|Hatch supports publishing to both test PyPI and PyPI
318
317
Version Control based versioning|✅ | Hatch offers `hatch_vcs` which is a plugin that uses setuptools_scm to support versioning using git tags. The workflow with `hatch_vcs` is the same as that with `setuptools_scm`.
319
318
Version bumping| ✅ | Hatch supports you bumping the version of your package using standard semantic version terms patch; minor; major
@@ -336,7 +335,7 @@ These include:
336
335
- Doesn't support dependency pinning
337
336
- Currently doesn't support use with other build back-ends. Lack of support for other build back-ends makes Hatch less desirable for users with more complex package builds. If your package is pure
338
337
Python, this won't be an issue. NOTE: there is a plan for this feature to be added in the upcoming months.
339
-
-Doesn't allow you to select what environment manager you use. <!-- (is this right??) -->
338
+
-Hatch won't by default support Conda environments. [But you can use a plugin for this!](https://github.com/OldGrumpyViking/hatch-conda).
340
339
- Hatch doesn't provide an end to end beginning workflow in it's documentation.
341
340
- Hatch, similar to PDM and Flit currently only has one maintainer.
Copy file name to clipboardExpand all lines: package-structure-code/python-package-distribution-files-sdist-wheel.md
+3-13Lines changed: 3 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,7 +26,9 @@ platform you use to manage your code.
26
26
27
27
**S**ource **D**istributions are referred to as sdist. As the name implies, a SDIST contains the source code; it has not been
28
28
built or compiled in any way. Thus, when a user installs your source
29
-
distribution using pip, pip needs to run a build step first. Sdist is normally stored as a `.tar.gz` archive (often called a "tarball").
29
+
distribution using pip, pip needs to run a build step first. For this reason, you could define a source distribution as a compressed archive that contains everything required to build a wheel (except for project dependencies) without network access.
30
+
31
+
Sdist is normally stored as a `.tar.gz` archive (often called a "tarball"). Thus, when a user installs your source distribution using pip, pip needs to run a build step first.
30
32
31
33
Below is an example sdist for the stravalib Python package:
32
34
@@ -89,18 +91,6 @@ items including a metadata directory and if you use `setuptools_scm` or `hatch_v
89
91
the SDist may also contain a file that stores the version.
90
92
```
91
93
92
-
<!--
93
-
* one of the benefits of wheel is pretty much avoiding setup.py which
94
-
has code mixed in. makes you more vulnerable to a code injection on install.
95
-
96
-
assuming this means if the package is already pre-built than setup.py isn't running anything on install because install is just moving files across to the machine to be run.
97
-
98
-
And having metadata separate allows someone to view the metadata without
99
-
running any python code as it's a machine and human readable format.
100
-
101
-
https://scikit-hep.org/developer/pep621
102
-
-->
103
-
104
94
### Wheel (.whl files):
105
95
106
96
A wheel file is a ZIP-format archive whose filename follows a specific format
Copy file name to clipboardExpand all lines: package-structure-code/python-package-structure.md
+23-3Lines changed: 23 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,18 @@
1
1
# Python Package Structure for Scientific Python Projects
2
2
3
-
## Directories that should be in all Python packages
3
+
## Directories that should be in your starting Python package repository
4
4
5
5
There are several core directories that should be included in all Python packages:
6
6
7
7
-**docs/:** discussed in our docs chapter, this directory contains your user-facing documentation website
8
8
-**tests/** this directory contains the tests for your project code
9
-
-**package-name/**: this is the directory that contains the code for your Python project. It is normally named using your project's name.
9
+
-**src/package-name/**: this is the directory that contains the code for your Python project. It is normally named using your project's name.
10
+
11
+
```{admonition} Multiple packages in a src/ folder
12
+
:class: tip
13
+
14
+
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.
15
+
```
10
16
11
17
## Src vs flat layouts
12
18
@@ -63,6 +69,16 @@ that includes tests within the **src/package-name** directory.
63
69
64
70
#### Pros of the src/ layout
65
71
72
+
```{admonition} How Python discovers and prioritizes importing modules
73
+
One of the main technical advantages of using the src/ layout, if you are just getting started with a new package,relates to how Python discovers packages. By default, Python adds a module in your current working directory to the front of the Python module search path.
74
+
75
+
This means that if you currently in your packages working directory, and your module code lives in the root e.g.: /package-name/module.py, python will discovers package-name/module.py before it the package as installed by pip or conda in a virtual environment.
76
+
77
+
However, if your package lives in a directory structure that is **src/package-name** then it won't be, by default, added to the Python path. This means that when you run import package, python will be forced to first search the active environment (which has your package installed).
78
+
79
+
Note that modern Python versions (3.11 and above) do have an option to adjust how the Python path finds modules (`PYTHONSAFEPATH`) however this is still a setting that a user would need to adjust in order to avoid the behavior of Python importing a module from your current working directory first.
80
+
```
81
+
66
82
The benefits of the **src/package-name** layout include:
67
83
68
84
- It ensures that tests always run on the installed version of your
@@ -79,7 +95,11 @@ will want to ensure that large tests datasets are not included in your package d
79
95
- The **src/package-name** layout is semantically more clear. Code is always found in the
80
96
**src/package-name** directory, `tests/` and `docs/`are in the root directory.
81
97
82
-
```{tip}
98
+
```{admonition} A few notes about the src/ layout
99
+
:class: tip
100
+
101
+
It is important to note here that sometimes when using the src/package-name 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/**.
102
+
83
103
* [Read more about reasons to use the **src/package-name** layout](https://hynek.me/articles/testing-packaging/)
0 commit comments