Skip to content

Commit bef6801

Browse files
committed
Fix: many more great comments to address
1 parent 68f5723 commit bef6801

File tree

6 files changed

+34
-72
lines changed

6 files changed

+34
-72
lines changed

index.md

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -128,35 +128,3 @@ Good meets the requirements. Going beyond the minimum can make package maintenan
128128

129129
This guide is now a work in progress. If you have ideas of things you'd like
130130
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.
161-
162-
-->

package-structure-code/intro.md

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,6 @@ package to be reviewed and accepted into our pyOpenSci open source ecosystem.
6161
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!
6262
```
6363

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/)
75-
* [PyPA packaging guide](https://packaging.python.org/en/latest/)
76-
```
77-
-->
78-
7964
```{toctree}
8065
:hidden:
8166
:caption: Package structure & code style

package-structure-code/pyproject-toml-python-package-metadata.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ of values. It has two keys that specify the build front end and back-end for a p
7979

8080
```
8181
[build-system]
82-
requires = ["setuptools>=45", "setuptools_scm[toml]>=6.2"]
82+
requires = ["setuptools>=45"]
8383
build-back-end = "setuptools.build_meta"
8484
8585
[project]

package-structure-code/python-package-build-tools.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,17 +168,16 @@ questions:
168168
- Flit, Hatch, PDM or Poetry (read below for more)
169169

170170
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.
173173

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.
176175

177176
## Python packaging tools summary
178177

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`).
180180

181-
Below, we summarize features offered by the most popular build front end tools.
182181
Note that because setuptools does not offer a front-end interface, it is not
183182
included in the table.
184183

@@ -313,7 +312,7 @@ using a tool like **Make** or **Nox**.
313312
314313
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.
315314
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).
317316
Publish to PyPI and test PyPI|✅|Hatch supports publishing to both test PyPI and PyPI
318317
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`.
319318
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:
336335
- Doesn't support dependency pinning
337336
- 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
338337
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).
340339
- Hatch doesn't provide an end to end beginning workflow in it's documentation.
341340
- Hatch, similar to PDM and Flit currently only has one maintainer.
342341

package-structure-code/python-package-distribution-files-sdist-wheel.md

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ platform you use to manage your code.
2626

2727
**S**ource **D**istributions are referred to as sdist. As the name implies, a SDIST contains the source code; it has not been
2828
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.
3032

3133
Below is an example sdist for the stravalib Python package:
3234

@@ -89,18 +91,6 @@ items including a metadata directory and if you use `setuptools_scm` or `hatch_v
8991
the SDist may also contain a file that stores the version.
9092
```
9193

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-
10494
### Wheel (.whl files):
10595

10696
A wheel file is a ZIP-format archive whose filename follows a specific format

package-structure-code/python-package-structure.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
# Python Package Structure for Scientific Python Projects
22

3-
## Directories that should be in all Python packages
3+
## Directories that should be in your starting Python package repository
44

55
There are several core directories that should be included in all Python packages:
66

77
- **docs/:** discussed in our docs chapter, this directory contains your user-facing documentation website
88
- **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+
```
1016

1117
## Src vs flat layouts
1218

@@ -63,6 +69,16 @@ that includes tests within the **src/package-name** directory.
6369

6470
#### Pros of the src/ layout
6571

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+
6682
The benefits of the **src/package-name** layout include:
6783

6884
- 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
7995
- The **src/package-name** layout is semantically more clear. Code is always found in the
8096
**src/package-name** directory, `tests/` and `docs/`are in the root directory.
8197

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+
83103
* [Read more about reasons to use the **src/package-name** layout](https://hynek.me/articles/testing-packaging/)
84104
```
85105

0 commit comments

Comments
 (0)