Skip to content

Clarify that packages don't need __init__.py - #1357

Merged
willingc merged 1 commit into
pypa:mainfrom
abravalheri:init-in-packages
Nov 11, 2023
Merged

Clarify that packages don't need __init__.py#1357
willingc merged 1 commit into
pypa:mainfrom
abravalheri:init-in-packages

Conversation

@abravalheri

Copy link
Copy Markdown
Contributor

This is a follow up on #1342: the tutorial implies that a Python package requires an __init__.py file.

I think that is the best to avoid this implication, otherwise we might "mis-educate" "packaging beginners" from the start.

The idea of this change is to introduce minimal rewording and a footnote.
The terms "regular package" and "namespace package" were introduced in PEP 420, which is also very intentional when it comes to equating regular and namespace packages:

A namespace package is not fundamentally different from a regular package. It is just a different way of creating packages. Once a namespace package is created, there is no functional difference between it and a regular package.

The change still recommends people that are getting started with Python packaging to add __init__.py as a result of the discussion in #1292: the general understanding in the community is that some tools might not fully support namespace packages yet (or be a bit more fussy about it) - although I cannot point out examples of these tools from the top of my mind...

Closes #1292.

But still recommend people that are getting started with Python
packaging to add them.

@willingc willingc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement. Thanks @abravalheri.

@willingc
willingc added this pull request to the merge queue Nov 11, 2023
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 11, 2023
@willingc
willingc added this pull request to the merge queue Nov 11, 2023
Merged via the queue into pypa:main with commit 54d4260 Nov 11, 2023
@abravalheri
abravalheri deleted the init-in-packages branch November 11, 2023 08:56
@webknjaz

Copy link
Copy Markdown
Member

Love the change, thanks!

@sinoroc

sinoroc commented Nov 11, 2023

Copy link
Copy Markdown
Contributor

Love the change

I don't :D

It is not a big deal, probably can be considered nitpicking, and you can feel free to ignore this.


Honestly, I do not follow the logic of this change.

__init__.py is recommended to import the directory as a regular package

It is not just a recommendation, it is the only way to have the package be a regular package. If the file is not there then it is imported as a namespace package, isn't it? Python still considers it as a namespace package, doesn't it? Maybe there are no practical differences in most of the cases, but the sentence is still incorrect. Maybe the PEP says that the initializer can be omitted but it seems to me very much against the spirit of the PEP (what its author stated later on [1] [2]).

Is the intention of mentioning that __init__.py files can be omitted to make it easier for beginners?

If I recall correctly I have more than once helped fix people's projects by telling them to add missing __init__.py.

the general understanding in the community is that some tools might not fully support namespace packages yet (or be a bit more fussy about it) - although I cannot point out examples of these tools from the top of my mind...

Unless the behavior of those has changes since last time I checked (which was a long time ago, for sure):

@abravalheri

abravalheri commented Nov 12, 2023

Copy link
Copy Markdown
Contributor Author

Hi, @sinoroc the idea was a bit different: the recommendation is for people to add __init__.py so that packages are considered "regular packages". I agree that this is the only way a package is considered regular.

We can further clarify that if you prefer.

I don't know about importlib, but setuptools discovery supports namespace packages (the distinction is explicit via 2 separated API though). However, I do tend to recomend people nowadays to go for autodiscovery if the can, which will find namespaces without further configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

__init__ should not be empty

6 participants