Why __init__.py File is Used in Python Projects | 2MinutesPy

  Рет қаралды 152,577

2MinutesPy

2MinutesPy

Күн бұрын

#__init__ #init #coding #script #python #pythonprogramming #pythonpackage #package #2minutespy
🖐Hey, want to know about __init__.py file in Python? In this Python tutorial, we dive into a fundamental concept: __init__.py file in Python.
Have you ever wondered about that mysterious "__init__.py" file lurking in your Python projects? In this video, we're peeling back the layers to reveal the secrets of the "__init__.py" file and why it plays a crucial role in your Python packages.
No jargon, no confusion - just a straightforward explanation of what "__init__.py" is, what it does, and why it's an essential component in your Python projects. Whether you're a beginner or a seasoned developer, understanding this file is key to mastering Python development. Let's dive in and demystify the "__init__.py" file together! 👩‍💻👨‍💻
More 2 Minutes Python Tutorial
if _name_ == "__main__" in Python: • What if __name__ == '_...
Python's seek() and tell() functions in 2 Minutes: • Understanding Python's...
- Python Threading in 2 Minutes: • Python Threading in 2 ...
- Python's super() function in 2 Minutes: • Python's super() Funct...
- How to Use *args and **kwargs in 2 Minutes: • How to Use *args and *...
- Python's ABC (Abstract Base Class) in 2 Minutes: • Python's ABC (Abstract...
- Python's _init_ Method in 2 Minutes: • Python's __init__ Meth...
Python's _getitem_ Method in 2 Minutes: • Python's __getitem__ M...
Subscribe to / @2minutespy for more such videos.
@2MinutesPy

Пікірлер: 169
@catbeatzzz5693
@catbeatzzz5693 7 ай бұрын
I love finding hidden gems like these on KZbin
@2MinutesPy
@2MinutesPy 7 ай бұрын
Thanks
@leez9753
@leez9753 7 ай бұрын
for some reason this showed up in my feed, good stuff my guy. keep it up
@2MinutesPy
@2MinutesPy 7 ай бұрын
Appreciate it
@dad30189
@dad30189 6 ай бұрын
I just found your channel from the KZbin recommendations. The 2 minute videos are a great idea!
@2MinutesPy
@2MinutesPy 6 ай бұрын
Thank you!
@HansLemurson
@HansLemurson 7 ай бұрын
We're __init__ to win-it!
@wamimuswamimus3496
@wamimuswamimus3496 4 ай бұрын
explanation is clear concise to the point.. no redundancy. love it.
@2MinutesPy
@2MinutesPy 4 ай бұрын
Thanks bruh
@nigh_anxiety
@nigh_anxiety 7 ай бұрын
Important detail that mixes people up a lot (and results in a lot of repeat questions on StackOverflow) --- If the scripts in your package rely on elements of other files in the same package via imports, they MUST use Relative imports. However, if you also have a main script or test script within your package that you want to run directly (as opposed to having an external main.py import your package), then that script within the package must use Absolute imports. Python determines whether or not it should be using relative or absolute imports based on whether or not the script it is processing is in a package, and the top-level script (the one invoked with 'python my_script.py') is never considered to be in a package while it is interpreted. If you insist on having scripts in your package that you want to run directly, put the intra-package imports in a try/except block, where you first try the relative import, catch an ImportError, and then try absolute imports.
@2MinutesPy
@2MinutesPy 7 ай бұрын
I must appreciate, that you took the time to explain handling imports within a package.
@phovos
@phovos 7 ай бұрын
I just had a 30-minute chat with GPT by pasting your comment and "huh?" --learning about asyncio.run(()) and namespace, now, haha.
@Gismo359
@Gismo359 6 ай бұрын
BS, you will be completely fine if you just use absolute imports. Relative imports only save you a tiny bit of time when moving modules to different packages. Otherwise, it's almost entirely up to personal preference.
@commentmachine1457
@commentmachine1457 4 ай бұрын
not really about the "MUST use relative imports statement within the package". According to PEP 8, "Absolute imports are recommended, as they are usually more readable and tend to be better behaved". Unless absolute import is very verbose (which should never occur in the first place as you should never yank something really deep out of another sub-package. Instead, you should have an API init script for the package and only import the ones from its init script). You almost should always use absolute import, because it explicitly states where the object comes from. This also makes it easier to move sub-packages around.
@MissingTricks
@MissingTricks 6 ай бұрын
Absolutely hidden information, I couldn’t found out this in udemy courses. Thank you🎉
@2MinutesPy
@2MinutesPy 5 ай бұрын
Glad it was helpful!
@patryk9073
@patryk9073 7 ай бұрын
I do a similar import without __init__.py file and it works fine. What is an example of import which wouldn't work without that empty file?
@2MinutesPy
@2MinutesPy 7 ай бұрын
Yeah, from Python 3.3, the namespace package won't need __init__.py file to import modules. But if you want to create a regular package, you'd include __init__.py file in your directory to make a better and maintainable package. The empty __init__.py file can be used to mark a directory as a package, which allows importing relative modules within the same package.
@chessfreak8813
@chessfreak8813 5 ай бұрын
@@2MinutesPy yes i also tried
@bide7603
@bide7603 4 ай бұрын
Pretty sure this is an ai video
@bivashy
@bivashy 4 ай бұрын
nah
@alias77799
@alias77799 3 ай бұрын
Yep
@theginix
@theginix Ай бұрын
It was still helpful
@lienjerry7370
@lienjerry7370 6 ай бұрын
__init__.py currently can be used to expose class or function. Taking the example in the video, if you want to expose the function "say_hello" in "greeting.py" to "my_package" level (i.e. in other arbitrary script, you want to "import say_hello from my_package"), you can write the line: "from .greeting import say_hello" in "__init__.py" to achieve this goal.
@2MinutesPy
@2MinutesPy 6 ай бұрын
Yeah, you can use directory-level import also to access say_hello function.
@user-dm2kp3vo2u
@user-dm2kp3vo2u 7 ай бұрын
So it's basically Python's way of declaring namespaces with optional super constructor functionality. That's what I understood. Great vid btw! :)
@Dude29
@Dude29 4 ай бұрын
Exactly!
@quangnhatle2512
@quangnhatle2512 7 ай бұрын
Somehow saw your vid by youtube recommended algorithm! I think I like the concept of explaining something really important and widely used in a short amount of time like 2-minutes! It was really good, maybe if more complex topics need to be explained then I think you dont need to keep a 2-min constraint. It can be extended up to 10min but not more than 10min. Maybe just keep it compact like this. Anyways great work !
@2MinutesPy
@2MinutesPy 7 ай бұрын
Glad you liked it and thanks for valuable suggestion.
@circulartext
@circulartext 6 ай бұрын
facts
@1LY4x8s96r
@1LY4x8s96r 4 ай бұрын
Nah. If he does that he can fall for the temptation to include filling material.
@Romogi
@Romogi 4 ай бұрын
Python is great. Burning things is greater.
@vasutke1187
@vasutke1187 7 ай бұрын
High clarity, Excellent Presentation and ultimate communication very interesting video. Thanks and Regards.
@2MinutesPy
@2MinutesPy 7 ай бұрын
So nice of you
@dheeraj3945
@dheeraj3945 4 ай бұрын
I can't thank you enough for this. This is the best explanation ever. Thank you very much
@2MinutesPy
@2MinutesPy 4 ай бұрын
Glad it was helpful!
@arvindh4327
@arvindh4327 7 ай бұрын
Also can help in abstraction and encapsulation(__all__) too
@Kothwalashivakumar
@Kothwalashivakumar 6 ай бұрын
Greatly explained in the most simplistic way possible. Thanks
@2MinutesPy
@2MinutesPy 6 ай бұрын
Glad it was helpful!
@Fizzulko
@Fizzulko 7 ай бұрын
Great video, keep making new ones. The way you explain things in 2 minutes is amazing ! Great work !
@2MinutesPy
@2MinutesPy 7 ай бұрын
Thanks a lot!
@moralfuxery
@moralfuxery 7 ай бұрын
Omg what an amazing video to come across while in my first weeks of using python. 👍👍👍
@2MinutesPy
@2MinutesPy 7 ай бұрын
Great to hear!
@circulartext
@circulartext 6 ай бұрын
python is super cool
@taiman9423
@taiman9423 7 ай бұрын
best one yet
@2MinutesPy
@2MinutesPy 7 ай бұрын
Thanks, mate
@tonyyin8735
@tonyyin8735 6 ай бұрын
Great video, short and concise!
@2MinutesPy
@2MinutesPy 6 ай бұрын
Much appreciated!
@epokal1
@epokal1 7 ай бұрын
This needs so much more engagement, also i might comment another just for engagement purposes
@2MinutesPy
@2MinutesPy 7 ай бұрын
Thanks for support.
@Aditya-ne4lk
@Aditya-ne4lk 4 ай бұрын
no. i want to gatekeep this
@markmilan57
@markmilan57 7 ай бұрын
Great startups! Keep on making more videos.
@2MinutesPy
@2MinutesPy 7 ай бұрын
Thank you, I will
@goofballbiscuits3647
@goofballbiscuits3647 5 ай бұрын
Awesome video!
@2MinutesPy
@2MinutesPy 5 ай бұрын
Thanks!
@baderhelsing6133
@baderhelsing6133 5 ай бұрын
Almost a day of searching why I'm getting "No module error" and I'm just missing init file for the imports folder, Udemy and coursera is missing these in their python selenium courses. Great video brother, you save my ass.
@2MinutesPy
@2MinutesPy 5 ай бұрын
Thanks
@SS-bo9yt
@SS-bo9yt 5 ай бұрын
Wow new format for me, thanks
@2MinutesPy
@2MinutesPy 5 ай бұрын
Glad to hear it!
@alpoo3627
@alpoo3627 6 ай бұрын
Thanks for your video!
@2MinutesPy
@2MinutesPy 6 ай бұрын
My pleasure!
@enkhboldnymdorj2790
@enkhboldnymdorj2790 5 ай бұрын
So surprisingly, its as same as index.ts or index.js in js packages.
@freepythoncode
@freepythoncode 6 ай бұрын
Thank you so much 🙂❤
@2MinutesPy
@2MinutesPy 6 ай бұрын
Always welcome
@GiovanniDeCillis
@GiovanniDeCillis 3 ай бұрын
I guess a tree structure diagram would help. But great video, straight to the point.
@2MinutesPy
@2MinutesPy 3 ай бұрын
Thanks
@jenniferstern324
@jenniferstern324 4 ай бұрын
Hi 😅 I'm a bit confused about one thing, sometimes I do "from math import sin, pi", so that I could use it as "sin(pi)", instead of "math.sin(pi)". Your example seems a bit different to me, as it doesn't import the function "say_hello()", but the whole python greetings.py file. Is there a way to only import the "say_hello()" function without needing to suffix it with "greetings.say_hello()"? (Sorry I'm a newb 😂)
@2MinutesPy
@2MinutesPy 4 ай бұрын
Yes you can do it. You just need to import only the say_hello() function from the greetings module. Like that👇 from my_package.greetings import say_hello Now you can directly call the say_hello() function. I hope that'll help you and sorry for late comment.
@luuu_na35
@luuu_na35 6 ай бұрын
Great! I can make my own package now.
@2MinutesPy
@2MinutesPy 6 ай бұрын
Great!
@prof-caio-jannuzzi
@prof-caio-jannuzzi 6 ай бұрын
Que top! Curti esse canal
@2MinutesPy
@2MinutesPy 6 ай бұрын
Obrigado
@shavebunny
@shavebunny 4 ай бұрын
Nice init.py right there, init.py?
@circulartext
@circulartext 6 ай бұрын
nice lesson
@2MinutesPy
@2MinutesPy 6 ай бұрын
Thanks! 😃
@HiltonFernandes
@HiltonFernandes 4 ай бұрын
Great video ! Congrats !
@HiltonFernandes
@HiltonFernandes 4 ай бұрын
If you don't mind, it would be nice to have another video explaining the initialization that __init__.py can do.
@2MinutesPy
@2MinutesPy 4 ай бұрын
Thank you very much!
@2MinutesPy
@2MinutesPy 4 ай бұрын
Sure, very soon
@abhisheknautiyal7956
@abhisheknautiyal7956 5 ай бұрын
Great video. I recommend making a playlist for easy access to all your related video.
@2MinutesPy
@2MinutesPy 5 ай бұрын
Thank you, I will
@dweepverma3662
@dweepverma3662 4 ай бұрын
Loved it
@2MinutesPy
@2MinutesPy 4 ай бұрын
Thanks
@ameyadali1768
@ameyadali1768 6 ай бұрын
Underrated
@epokal1
@epokal1 7 ай бұрын
I love this
@2MinutesPy
@2MinutesPy 7 ай бұрын
Great...
@oliveselow-bw3py
@oliveselow-bw3py 6 ай бұрын
Like it..!
@2MinutesPy
@2MinutesPy 6 ай бұрын
Glad you like it!
@kahvuongng2701
@kahvuongng2701 2 күн бұрын
ty
@oliviarojas7023
@oliviarojas7023 4 ай бұрын
Just BTW. . Use namespace . . Pep 420 . . __init__.Py is not used after python 3.3 for package imports over namespace. . It has some functionality, but a pretty old change.
@onepun9583
@onepun9583 5 ай бұрын
what's the full syntax to implement it?
@AnweshAdhikari
@AnweshAdhikari 6 ай бұрын
@vasutke1187
@vasutke1187 7 ай бұрын
Sir, If possible Simultaneous video on SQL. Regards.
@2MinutesPy
@2MinutesPy 7 ай бұрын
As soon as possible
@IcyyDicy
@IcyyDicy 7 ай бұрын
This feels AI generated The voice has weird intonation
@2MinutesPy
@2MinutesPy 7 ай бұрын
Sorry for inconvenience caused
@JohnintheTyranny
@JohnintheTyranny 29 күн бұрын
Doesn't it do the same thing if you create a class in any other file?
@uuuummm9
@uuuummm9 4 ай бұрын
I did not become more clear at all. Sometimes importing works and sometimes it does not. It would be more useful to explain how python interpreter searches for the packages and makes them available for importing. I have a code base that works in 3 ways: as a FastApi app, as pytest testing the web app and also as part of a databricks job. I was able to make first two cases to work more or kess clean (although I had to follow a particular folder structure and if I change it everything breaks, so I cannot say I understand how it works). And for databricks i ended up with adding some ugly "sys.path.add" or like that to make it work. 4 years of working with python and those init files are still a mystery for me.
@kunalsoni7681
@kunalsoni7681 6 ай бұрын
this video made me very clear about the python package concept with a practical example
@2MinutesPy
@2MinutesPy 6 ай бұрын
Thanks
@venil82
@venil82 5 ай бұрын
These double underlined magic methods is such a terrible design
@theginix
@theginix Ай бұрын
Why?
@khanbasharat
@khanbasharat 5 ай бұрын
New subscriber here!
@2MinutesPy
@2MinutesPy 5 ай бұрын
Welcome!!
@kerimalpalt
@kerimalpalt 7 ай бұрын
Because the person who wrote it is from South London
@emman100
@emman100 7 ай бұрын
🤣💀
@Arvine_Lyrics
@Arvine_Lyrics 4 ай бұрын
It's used mainly to prevent circular imports😊
@2MinutesPy
@2MinutesPy 4 ай бұрын
Nowadays, this file is not needed as Python is advancing but yeah, you can prevent circular imports within your projects using this file.
@RocketPropelledWombat
@RocketPropelledWombat 4 ай бұрын
The classics....
@Thekingslayer-ig5se
@Thekingslayer-ig5se 2 ай бұрын
Man your an icon
@tyrojames9937
@tyrojames9937 6 ай бұрын
😁😁
@vishalvishwakarma4389
@vishalvishwakarma4389 5 ай бұрын
This works even when there is no init.py file
@2MinutesPy
@2MinutesPy 5 ай бұрын
Yeah, if you have a Python version above 3.3
@vladhaidukkk-learning
@vladhaidukkk-learning 5 ай бұрын
As far as I know, this file became optional after Python 3.3
@2MinutesPy
@2MinutesPy 5 ай бұрын
Yeah
@kvnptl4400
@kvnptl4400 4 ай бұрын
Sometimes For me it's 1MinutePy
@kuljeetkumar4657
@kuljeetkumar4657 4 ай бұрын
Content is top notch in 2 mins. Ads take 30 seconds. That is the only bad part.
@2MinutesPy
@2MinutesPy 4 ай бұрын
Thanks!
@africknchickn_YT
@africknchickn_YT 5 ай бұрын
AI generated?
@udonwadon2045
@udonwadon2045 5 ай бұрын
the amount of quirks in python that is senseless makes me prefer ruby.
@flosrv3194
@flosrv3194 2 ай бұрын
I always try it and nothing works for my projects
@2MinutesPy
@2MinutesPy 2 ай бұрын
ohh
@user-en7dl1lv6b
@user-en7dl1lv6b 4 ай бұрын
It’s a module
@HussinJaafari
@HussinJaafari 4 ай бұрын
Since 3.9, you no more need the __init__.py anymore
@2MinutesPy
@2MinutesPy 4 ай бұрын
Yeah
@fqidz
@fqidz 7 ай бұрын
i cant tell if the voice is ai or a real person
@alex11943
@alex11943 6 ай бұрын
1:11 is a dead giveaway that it's AI voice. But at least the explanation is good
@forlorn4523
@forlorn4523 10 күн бұрын
I am able import methods without creating the init file
@2MinutesPy
@2MinutesPy 10 күн бұрын
Yes, you can in newer versions of Python. But not putting __init__.py file sometimes cause circular imports error within modules.
@Carhill
@Carhill 7 ай бұрын
Narration is clearly text-to-speech, but the dialogue itself feels like it was spat out by a LLM. Awful.
@user-uc6wo1lc7t
@user-uc6wo1lc7t 4 ай бұрын
Emmm... That "init" shit never works for me... How tf any python file outside "my_package" can know about that package? It's not even in sys.path... I'm really confused. EVERYTHING I try to make sense of this init file just never works. For me its just a garbage. Personally I just create and activate venv in ".venv" folder and inside this folder create file "any_name.pth" with "../" content. So I have root directory with ".venv" subdirectory. And when I activate my venv, my root folder is inside my sys.path. so, in project, I can use absolute imports as I want. And if I want to use it as a package - I just create pyproject.toml file, configure it and use pip install with path\to\ package. And that's much better, because you can do the same thing even using git repos... I don't know wtf is __init__.py... Just a garbage
@VahidOnTheMove
@VahidOnTheMove 7 ай бұрын
Well, how this video is useful that's just basically says put an __init__.py!? You can literally get the info on the internet in less than 2 mins. I need to know what I can do by adding lines of codes in __init__
@2MinutesPy
@2MinutesPy 7 ай бұрын
Yeah right
@murphygreen8484
@murphygreen8484 5 ай бұрын
Isn't this no longer needed as of python 3.3?
@2MinutesPy
@2MinutesPy 5 ай бұрын
Yeah, you're right but a developer should be aware of this.
@onehotseat
@onehotseat 4 ай бұрын
Great vid, only issue is your pronunciation of init. It's "i nit", with emphasis on the "nit". Not "in it" with emphasis on the "in".
@2MinutesPy
@2MinutesPy 4 ай бұрын
Noted and thanks
@royk7712
@royk7712 6 ай бұрын
why because the brit say it often like "init bruv?" jk
@TeraBaap-pc1dd
@TeraBaap-pc1dd 7 ай бұрын
Susbscribed on 999 The next one would be 1k directly
@2MinutesPy
@2MinutesPy 7 ай бұрын
Thanks
@s1mo
@s1mo 6 ай бұрын
bri'ish people when they write the __init__.py file
@mihirjog2707
@mihirjog2707 4 ай бұрын
That's useful, init?
@carlosmspk
@carlosmspk 4 ай бұрын
Except python no longer needs __init__.py and works perfectly fine without it. I was hoping this video would address this :(
@2MinutesPy
@2MinutesPy 4 ай бұрын
Yeah and sorry
@shubhamjha9082
@shubhamjha9082 4 ай бұрын
🙄 😅
@ifstatementifstatement2704
@ifstatementifstatement2704 6 ай бұрын
Haven’t used it yet but from what I’ve seen it seems to be a pretentious c++ header file wannabe.
@geoafrikana
@geoafrikana 5 ай бұрын
That escalated quickly
@zeez7777
@zeez7777 4 ай бұрын
Man i really miss the times when i didnt have to listen to these soulless AI voices that completely distract from the actual thing being discussed
@feazysil2707
@feazysil2707 5 ай бұрын
You can't make a 2:40 video with 16s intro everyone that clicked this video know you are talking about init no need to state it
@2MinutesPy
@2MinutesPy 5 ай бұрын
Noted
@gaot5281
@gaot5281 5 ай бұрын
If I put the file test.py in a folder it did not work. The error is "ModuleNotFoundError: No module named "my_package"
@schuylergood4897
@schuylergood4897 6 ай бұрын
This looks AI generated
@joseph2625
@joseph2625 5 ай бұрын
PEP420 was drafted over a decade ago. Are you just pretending that it doesn't exist?
@Parodyyy
@Parodyyy 2 ай бұрын
What?
@RyanWaite28
@RyanWaite28 6 ай бұрын
I like Node.JS better 🤷🏾‍♂️
@2MinutesPy
@2MinutesPy 6 ай бұрын
No worries, you can try Python also sometimes
@marcopulido3087
@marcopulido3087 5 ай бұрын
__init__.py is a kind of gluon 😂
@maudjito
@maudjito 4 ай бұрын
Hate the AI voice
@komodo-dragon
@komodo-dragon 4 ай бұрын
Bc python is british
@2MinutesPy
@2MinutesPy 4 ай бұрын
😁
@pooria10001
@pooria10001 2 ай бұрын
Why should we add an additional and ridiculous file called __init__.py in every folder in the project to recognize a folder as a package ?!!!! It's really ridiculous! STUPID PYTHON.
@wagbagsag
@wagbagsag 7 ай бұрын
Text to speech. Bleh
@susugar3338
@susugar3338 7 ай бұрын
It's not a big problem, man. At least it's easy for my bad English to understand more than native English voice.
@wagbagsag
@wagbagsag 7 ай бұрын
In general I agree! Not everyone is cut out be a narrator. But the information density of this content is so low that it feels straight out of chat gpt, and the text-to-speech emphasizes that appearance.
5 Good Python Habits
17:35
Indently
Рет қаралды 430 М.
Жайдарман | Туған күн 2024 | Алматы
2:22:55
Jaidarman OFFICIAL / JCI
Рет қаралды 1,8 МЛН
When You Get Ran Over By A Car...
00:15
Jojo Sim
Рет қаралды 28 МЛН
Can You Draw A PERFECTLY Dotted Line?
00:55
Stokes Twins
Рет қаралды 114 МЛН
Why I focus on patterns instead of technologies
7:55
NeetCodeIO
Рет қаралды 205 М.
Intermediate Python Tutorial: How to Use the __init__.py File
6:22
Eric O Meehan
Рет қаралды 129 М.
How To Use List Comprehension In Python
6:41
Taylor's Software
Рет қаралды 6 М.
Python init.py File: Best Practices and Customizations
7:12
CodersLegacy
Рет қаралды 557
The Fastest Way to Loop in Python - An Unfortunate Truth
8:06
mCoding
Рет қаралды 1,4 МЛН
3 PYTHON AUTOMATION PROJECTS FOR BEGINNERS
17:00
Internet Made Coder
Рет қаралды 1,5 МЛН
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 277 М.
The Absolute Best Intro to Monads For Software Engineers
15:12
Studying With Alex
Рет қаралды 607 М.
Enums considered harmful
9:23
Matt Pocock
Рет қаралды 198 М.
Сколько реально стоит ПК Величайшего?
0:37
iPhone socket cleaning #Fixit
0:30
Tamar DB (mt)
Рет қаралды 11 МЛН
Hisense Official Flagship Store Hisense is the champion What is going on?
0:11
Special Effects Funny 44
Рет қаралды 3,1 МЛН
Easy Art with AR Drawing App - Step by step for Beginners
0:27
Melli Art School
Рет қаралды 13 МЛН
Мой инст: denkiselef. Как забрать телефон через экран.
0:54
Хотела заскамить на Айфон!😱📱(@gertieinar)
0:21
Взрывная История
Рет қаралды 6 МЛН