What should you write into the __init__.py file? 2MinutesPy

preview_player
Показать описание
In this video, we will discuss what code and data you can put into your __init__.py file in Python.

You know without this file, Python won't recognize the directory as a package, and you won't be able to import its modules. So, the question is, “What to include” in your __init__.py file?

Well, the list is endless, but here are something you can include:
- Import specific functions and submodules
- Code that you want to execute when the package is initialized
- __all__ variable to control the functions and variable imported when someone uses wildcard import statement
- Module's information
- Logging capability
- Environment variables
- External connections
- and much more...

Chapters:
⏩ 0:00 Intro
⏩ 0:24 Directory structure and __init__.py file
⏩ 0:50 What to include in __init__.py file
⏩2:53 Outro

✨More on 2MinutesPy👇👇

@2MinutesPy

#__init__.py #init #__init__ #pythonpackage #package #python #pythonprogramming #module #script #2minutespy
Рекомендации по теме
Комментарии
Автор

Putting logic in the __init__.py file sounds like an excellent opportunity for a "wtf is my code doing"-moment in a later debug session. Constants could be nice.

AntonioRonde
Автор

Notice that he imported functions and not whole modules. Be careful when including whole modules since that can cause "circular import" errors. It's probably best practice not to import whole modules, but wanted to throw that nugget of personal experience out.

blakephillips
Автор

Never ever ever put side effects and global variables to your __init__.py file

TheTmLev
Автор

Something to be wary of is that every single module in a package has an implicit dependency on the __init__.py file, so re-exporting modules actually creates a fake dependency where every module in the package requires every other module in the package, leaving you with very subtle import loops. Because of this, I don't recommend re-exporting modules in __init__.py files at all. The modules should depend on the __init__.py files, not the other way around.

FastKnight
Автор

Now, another thing I can flex to my teammates of Junior Devs ;) Thanks

spidey
Автор

Hey, fantastic animations! Which tool did you use to create them?

oussaber
Автор

Isn't there also a thing where you check if the __name__ == "__main__" to see if something is being run as a program or a module?

fortnort
Автор

Credentials and tokens too, but only if you are a true member of the python gang

franlopez
Автор

The fact that this video has a length of 3.14

nelsonlee
Автор

I am new in python, a year more or less, I used to program in perl. Anyway, I like the idea of logging modeule in init. Any advice about that? Sorry my english, si not my native lang. Thanks for the video.

Nkrodamus
Автор

Delete a random file from system32 if you are using it on Windows.

trevoro.
Автор

2minPY ... don't become 3minPY pls / Remember around 2min is the attention span of your subscribers...

neckbro
Автор

👍 (normaly Id say a lot of stuff but my battery is all time low)

compositeboson
Автор

Actually from Python 3.3 you don't need to use __init__.py file

acopier
Автор

this video isn't two minutes, very disappointed

pepelover
join shbcf.ru