Inversus
Inversus

Reputation: 3175

What other special modules (files) exist at the package level?

I know of only two filenames that will be interpreted meaningfully: __init__.py and __main__.py

__init__.py: The init file for a package. Executed when a package is first imported/imported from. If this file is present, then the directory is considered a package (there also exists "namespace packages" that don't have init modules)

__main__.py: The main entry point for a package. This file is executed when running the package from the commandline. (ex: python -m PackageName)

What other files can exist and what do they do?

Upvotes: 5

Views: 2151

Answers (1)

Dimitris Fasarakis Hilliard
Dimitris Fasarakis Hilliard

Reputation: 160567

I don't believe there's definite proof of only __main__ and __init__ being the only special files, definitely haven't seen any documentation for it. But, there's a decent way of asserting that the probability of those being the only ones is high by searching for dunders in the file finder for the CPython repo.

Only __init__.py's and __main__.py's; @chepner's point is also highlighted by the sole other dunder: __future__.py, other usages of __*__.py should indeed not be used as it might conflicts with some future Python file.

Since I had the source around, grepping for "__.*__.py" in all files didn't yield any other result other than the aforementioned trio.

Upvotes: 6

Related Questions