r/learnpython • u/BigBootyBear • Jan 07 '24
Whats the ruling on using __init__.py in 2024?
Every time I am having a module import problem, I come across someone mentioning init.py, only to be reminded by a (much later SO answer) that it's no longer needed in Python 3. I often don't know who to believe.
What's the final ruling the utility of init.py in 2024 greenfield projects (i.e. no legacy code)?
33
Upvotes
37
u/shiftybyte Jan 07 '24
You no longer need it to import something from a folder.
You can use it if you want to better control what gets imported when a folder gets imported.
31
u/socal_nerdtastic Jan 07 '24
We use it when we want a folder to act like a file. So if we have a folder named
utilsand we want to doThen we can use
__init__.pyto define what exactly gets imported.If you have a file in the utils folder named "data.py", and you want to import that file, you can just do
This is the change. In legacy code you would need an empty
__init__.pyin the folder for that to work.In short, you no longer need an empty
__init__.pyfile. But you still may want to use one and put some code in it.12
u/nameloCmaS Jan 07 '24
I believe pytest still requires an __init__.py (empty or otherwise) to automatically load folders and work out the folder structure.
Other packages may have similar requirements.
This is the only reason I have them.
Edit: formatting.
8
u/boat-la-fds Jan 08 '24
If you don't have an
__init__.pyfile in a package,python setup.py bdist_wheelwon't add the package in the resulting wheel file.13
u/zurtex Jan 08 '24 edited Jan 08 '24
Unless you really know what you're doing please use
__init__.pyfor any directory you intend to import from, it's fine even if it's just an empty file.Otherwise you're creating Implicit Namespace Packages, and they are very hard to reason about. And unless you look at the words "Dynamic Path Computation" and think that's a good idea for your import strategy avoid it.
Here's an example, let's say two projects are on your PYTHONPATH, and have an implicit namespace package
foo, and project 1 hasbar.pyin it's foo folder and project 2 hasbaz.pyin it's foo folder.When you run
from foo import baryou are importing from project 1, when you runfrom foo import bazyou are importing from project 2.If project 1 creates a
baz.pywhen you runfrom foo import bazyou might import from project 1 or 2, it depends whether project1 appeared earlier on the the PYTHONPATH or not.If project 2 creates an
__init__.pyin it's foo folder now when you runfrom foo import baryou get an import error.Now there are real use cases for Implicit Namespace Packages, that's why they exist, but please don't use them blindly, read the docs and motivation: https://peps.python.org/pep-0420/
4
u/billsil Jan 08 '24
As soon as you start using relative imports, it helps to have them.
2
u/SisyphusAndMyBoulder Jan 08 '24
I like it. Helps with imports, and PyCharm seems to work really well with it versus without