loading...

Don't omit __init__.py

methane profile image Inada Naoki Updated on ・1 min read

I saw some people think Python 3 allow package without __init__.py on Stackoverflow or mailing lists.

This is misunderstanding. Package directory without __init__.py is "namespace package", not a regular package. See PEP 420 for reference.

Namespace package is special package for people creating complex "distribution package" ("package" which can be registered on PyPI). There is guide for namespace package in "Python Packaging User guide".

There are some difference between namespace package and regular package.

For example, unittest module in standard library doesn't search into directory without __init__.py. If they do, they may take dozen seconds to search tests in node_modules which contains millions of files and subdirectories.

You should not omit __init__.py in your project unless you are 100% sure about what "namespace package" is. Use "regular package" with __init__.py instead.

Discussion

pic
Editor guide
Collapse
diegomgar profile image
Dieg Oto

Thanks, it is interesting but I did not understood shit about the difference of those package you say.

Can you extend your explanation?

Ty

Collapse
methane profile image
Inada Naoki Author

No need to understand "namespace package" unless you're distributing complex package.

If you are, read user guide.

Collapse
diegomgar profile image