-
Notifications
You must be signed in to change notification settings - Fork 1.2k
#1316 Update packaging-projects.rst with a tip on good module naming #1424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LLyaudet
wants to merge
33
commits into
pypa:main
Choose a base branch
from
LLyaudet:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
17e76be
Update packaging-projects.rst with a tip on good module naming
LLyaudet 1815996
Update packaging-projects.rst add empty line after items
LLyaudet 24d81e5
Update packaging-projects.rst ???
LLyaudet b8890ba
Update packaging-projects.rst use intersphinx
LLyaudet 74af3e9
Merge branch 'pypa:main' into patch-1
LLyaudet 2779b98
Update packaging-projects.rst use correctly intersphinx
LLyaudet c12f338
LL: create discussion choosing-a-module-name and link between it and …
LLyaudet cc8ffd5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 614bf97
LL: correction nox build.
LLyaudet 5f7790e
Update source/discussions/choosing-a-module-name.rst
LLyaudet 408d7aa
Update source/discussions/choosing-a-module-name.rst
LLyaudet 2893273
LL: correction code-block python -> pycon.
LLyaudet 81d8d52
LL : more markup in choosing-a-module-name.
LLyaudet 43e64d9
LL: add warning and ref to PEP8 for unnatural module names.
LLyaudet 76bb7bf
LL: add import package since import packages are a special case of mo…
LLyaudet 4e5053e
LL: clarify terminology with links to the glossary.
LLyaudet fd7909e
Merge branch 'pypa:main' into patch-1
LLyaudet 8f314ae
LL: typo cumbersone -> cumbersome.
LLyaudet 8115dae
LL: correction Title underline too short.
LLyaudet 63b5b46
Merge branch 'main' into patch-1
LLyaudet 23e289e
Update source/discussions/choosing-a-package-module-name.rst
LLyaudet de7fbfa
LL: add recommendation to have only one importable package.
LLyaudet f962554
Update source/discussions/choosing-a-package-module-name.rst
LLyaudet b5fdb5c
LL: PyPI back after applying suggestion.
LLyaudet b93f306
LL: partial rewrite following in part suggestions by sinoroc.
LLyaudet a1745ef
LL: add section on the consequences of naming for consistency of the …
LLyaudet dfe196d
LL: refs for build backends.
LLyaudet 7a52305
LL: missing new line before pycon.
LLyaudet 8a20cac
LL: another missing new line.
LLyaudet eecfc2f
LL: correction `dist-info` -> ``dist-info``.
LLyaudet 7d29053
LL: correct name normalization ref that fails in nox.
LLyaudet fcff555
LL: warning about 2 types of name normalization.
LLyaudet 401d037
LL: naming gymnastic at the end with a little joke :).
LLyaudet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
Choosing a Package/Module Name | ||
============================== | ||
|
||
This discussion is a complement to :doc:`/tutorials/packaging-projects`. | ||
|
||
Make sure to choose a valid :ref:`Python identifier <python:identifiers>` for the names of all your :term:`import packages <import package>` and :term:`modules <module>`. | ||
The name of the :term:`project` and the name of the :term:`distribution package` are one and the same. | ||
It is the name you will see on PyPI, for example. | ||
The name of the project and the name of the top-level :term:`import package` (or :term:`module`) can be different. | ||
|
||
Moreover, one PyPI project/dist may ship more than one module or importable package — it is only possible that one matches the name, others can't. | ||
It is recommended to have only one importable package, with a name as similar as possible as the ``dist-info`` file in the installation folder. | ||
|
||
Project names (usually found in :file:`pyproject.toml`) and import package names follow different rules and conventions. | ||
The normalized form (and thus the preferred form) for project names | ||
is the so-called "kebab case" (see :doc:`/specifications/name-normalization`), for example ``abcd-1234``. | ||
But import packages and modules should have a valid Python identifier as a name. | ||
|
||
With an import package name ``abcd-1234``, the following would not work: | ||
|
||
.. code-block:: pycon | ||
|
||
>>> import abcd-1234 | ||
>>> from abcd-1234 import something | ||
|
||
Since ``abcd-1234`` is not a valid Python identifier. | ||
(Importing such a module would be cumbersome, completely unnatural and against the long-established conventions in the Python community; | ||
see, for example, :pep:`8#package-and-module-names`. | ||
There is a way to import it anyway, see :doc:`importlib <python:library/importlib>` and this question_.) | ||
|
||
:ref:`Python identifiers <python:identifiers>` follow the so-called "snake case". | ||
The preferred form for an import package name is ``abcd_1234`` which is a valid Python identifier. | ||
Note the underscore ``_`` as separation character instead of of the dash ``-`` seen in the project name. | ||
|
||
Having a directory structure with ``src/abcd_1234/`` instead of ``src/abcd-1234/`` has 2 consequences: | ||
|
||
- The following works: | ||
|
||
.. code-block:: pycon | ||
|
||
>>> import abcd_1234 | ||
>>> from abcd_1234 import something | ||
|
||
- All four build backends covered in the tutorial :doc:`/tutorials/packaging-projects` will work: | ||
|
||
- Flit will not crash with an error; | ||
- Hatch will recognize that the module corresponding to the package is ``abcd_1234`` instead of defaulting to ``src`` and building a not working wheel. | ||
|
||
More information about :doc:`Python imports <python:reference/import>` and its :doc:`grammar <python:reference/grammar>`. | ||
|
||
Another aspect of choosing a package/module name is the naming consistency | ||
of the files generated by the build backend. | ||
With the four build backends covered in the tutorial | ||
(:ref:`Flit <flit>`, :ref:`Hatchling <hatch>`, :ref:`PDM <pdm>`, and :ref:`setuptools`), | ||
when the correct import package is found, its name is always leaved as is. | ||
|
||
But, starting from the project name, the build backend also generates: | ||
|
||
- an archive file like ``abcd_1234-1.0.0.tar.gz`` with Flit, Hatchling, and PDM, | ||
or ``abcd-1234-1.0.0.tar.gz`` with setup-tools; | ||
here we can see that some backends but not all | ||
apply a normalization on the project name: lowercase, underscore ("snake case") instead of hyphen; | ||
note that we do not talk about the same normalization referenced at the beginning of this document | ||
(:doc:`/specifications/name-normalization`); | ||
- a wheel like ``abcd_1234-1.0.0-py3-none-any.whl`` with all build backends; | ||
- a dist-info like ``abcd_1234-1.0.0.dist-info`` with all build backends; | ||
- in the case of setuptools, an egg-info like ``abcd_1234-1.0.0.egg-info``. | ||
|
||
Thus, except for the archive with setuptools, all prefixes of filenames | ||
use a normalization of project name. | ||
This normalization will match an import package name chosen consistently | ||
by normalization of the project name. | ||
You may see an interest in installing a wheel named ``abcd_1234-1.0.0-py3-none-any.whl``, | ||
and having as a result ``abcd_1234-1.0.0.dist-info`` and ``abcd_1234/`` (or ``abcd_1234.py``) in your installation directory :) | ||
(instead of a project ``aBcD-1234``, a wheel ``abcd_1234-1.0.0-py3-none-any.whl``, installing ``abcd_1234-1.0.0.dist-info`` and ``4-3-2-1cDbA/`` ;) ). | ||
|
||
.. _question: https://stackoverflow.com/questions/8350853/how-to-import-module-when-module-name-has-a-dash-or-hyphen-in-it |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.