Тёмный

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

2MinutesPy
Подписаться 21 тыс.
Просмотров 157 тыс.
50% 1

Опубликовано:

 

8 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 171   
@catbeatzzz5693
@catbeatzzz5693 9 месяцев назад
I love finding hidden gems like these on RU-vid
@2MinutesPy
@2MinutesPy 9 месяцев назад
Thanks
@dad30189
@dad30189 8 месяцев назад
I just found your channel from the RU-vid recommendations. The 2 minute videos are a great idea!
@2MinutesPy
@2MinutesPy 8 месяцев назад
Thank you!
@leez9753
@leez9753 9 месяцев назад
for some reason this showed up in my feed, good stuff my guy. keep it up
@2MinutesPy
@2MinutesPy 9 месяцев назад
Appreciate it
@user-dm2kp3vo2u
@user-dm2kp3vo2u 8 месяцев назад
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 6 месяцев назад
Exactly!
@nigh_anxiety
@nigh_anxiety 9 месяцев назад
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 9 месяцев назад
I must appreciate, that you took the time to explain handling imports within a package.
@phovos
@phovos 9 месяцев назад
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 8 месяцев назад
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 6 месяцев назад
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 7 месяцев назад
Absolutely hidden information, I couldn’t found out this in udemy courses. Thank you🎉
@2MinutesPy
@2MinutesPy 7 месяцев назад
Glad it was helpful!
@quangnhatle2512
@quangnhatle2512 9 месяцев назад
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 9 месяцев назад
Glad you liked it and thanks for valuable suggestion.
@circulartext
@circulartext 8 месяцев назад
facts
@1LY4x8s96r
@1LY4x8s96r 6 месяцев назад
Nah. If he does that he can fall for the temptation to include filling material.
@wamimuswamimus3496
@wamimuswamimus3496 6 месяцев назад
explanation is clear concise to the point.. no redundancy. love it.
@2MinutesPy
@2MinutesPy 6 месяцев назад
Thanks bruh
@HansLemurson
@HansLemurson 9 месяцев назад
We're __init__ to win-it!
@patryk9073
@patryk9073 9 месяцев назад
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 9 месяцев назад
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 6 месяцев назад
@@2MinutesPy yes i also tried
@vasutke1187
@vasutke1187 9 месяцев назад
High clarity, Excellent Presentation and ultimate communication very interesting video. Thanks and Regards.
@2MinutesPy
@2MinutesPy 9 месяцев назад
So nice of you
@moralfuxery
@moralfuxery 9 месяцев назад
Omg what an amazing video to come across while in my first weeks of using python. 👍👍👍
@2MinutesPy
@2MinutesPy 9 месяцев назад
Great to hear!
@circulartext
@circulartext 8 месяцев назад
python is super cool
@lienjerry7370
@lienjerry7370 8 месяцев назад
__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 8 месяцев назад
Yeah, you can use directory-level import also to access say_hello function.
@dheeraj3945
@dheeraj3945 6 месяцев назад
I can't thank you enough for this. This is the best explanation ever. Thank you very much
@2MinutesPy
@2MinutesPy 6 месяцев назад
Glad it was helpful!
@epokal1
@epokal1 9 месяцев назад
This needs so much more engagement, also i might comment another just for engagement purposes
@2MinutesPy
@2MinutesPy 9 месяцев назад
Thanks for support.
@Aditya-ne4lk
@Aditya-ne4lk 6 месяцев назад
no. i want to gatekeep this
@taiman9423
@taiman9423 9 месяцев назад
best one yet
@2MinutesPy
@2MinutesPy 9 месяцев назад
Thanks, mate
@bide7603
@bide7603 6 месяцев назад
Pretty sure this is an ai video
@bivashy
@bivashy 5 месяцев назад
nah
@alias77799
@alias77799 5 месяцев назад
Yep
@theginix
@theginix 3 месяца назад
It was still helpful
@baderhelsing6133
@baderhelsing6133 7 месяцев назад
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 7 месяцев назад
Thanks
@Kothwalashivakumar
@Kothwalashivakumar 8 месяцев назад
Greatly explained in the most simplistic way possible. Thanks
@2MinutesPy
@2MinutesPy 8 месяцев назад
Glad it was helpful!
@Fizzulko
@Fizzulko 9 месяцев назад
Great video, keep making new ones. The way you explain things in 2 minutes is amazing ! Great work !
@2MinutesPy
@2MinutesPy 9 месяцев назад
Thanks a lot!
@arvindh4327
@arvindh4327 8 месяцев назад
Also can help in abstraction and encapsulation(__all__) too
@markmilan57
@markmilan57 9 месяцев назад
Great startups! Keep on making more videos.
@2MinutesPy
@2MinutesPy 9 месяцев назад
Thank you, I will
@Romogi
@Romogi 6 месяцев назад
Python is great. Burning things is greater.
@SS-bo9yt
@SS-bo9yt 7 месяцев назад
Wow new format for me, thanks
@2MinutesPy
@2MinutesPy 7 месяцев назад
Glad to hear it!
@GiovanniDeCillis
@GiovanniDeCillis 5 месяцев назад
I guess a tree structure diagram would help. But great video, straight to the point.
@2MinutesPy
@2MinutesPy 5 месяцев назад
Thanks
@jenniferstern324
@jenniferstern324 6 месяцев назад
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 6 месяцев назад
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.
@goofballbiscuits3647
@goofballbiscuits3647 7 месяцев назад
Awesome video!
@2MinutesPy
@2MinutesPy 7 месяцев назад
Thanks!
@enkhboldnymdorj2790
@enkhboldnymdorj2790 7 месяцев назад
So surprisingly, its as same as index.ts or index.js in js packages.
@epokal1
@epokal1 9 месяцев назад
I love this
@2MinutesPy
@2MinutesPy 9 месяцев назад
Great...
@vasutke1187
@vasutke1187 9 месяцев назад
Sir, If possible Simultaneous video on SQL. Regards.
@2MinutesPy
@2MinutesPy 9 месяцев назад
As soon as possible
@tonyyin8735
@tonyyin8735 8 месяцев назад
Great video, short and concise!
@2MinutesPy
@2MinutesPy 8 месяцев назад
Much appreciated!
@oliviarojas7023
@oliviarojas7023 6 месяцев назад
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.
@abhisheknautiyal7956
@abhisheknautiyal7956 7 месяцев назад
Great video. I recommend making a playlist for easy access to all your related video.
@2MinutesPy
@2MinutesPy 7 месяцев назад
Thank you, I will
@kunalsoni7681
@kunalsoni7681 8 месяцев назад
this video made me very clear about the python package concept with a practical example
@2MinutesPy
@2MinutesPy 8 месяцев назад
Thanks
@circulartext
@circulartext 8 месяцев назад
nice lesson
@2MinutesPy
@2MinutesPy 8 месяцев назад
Thanks! 😃
@alpoo3627
@alpoo3627 8 месяцев назад
Thanks for your video!
@2MinutesPy
@2MinutesPy 8 месяцев назад
My pleasure!
@shavebunny
@shavebunny 6 месяцев назад
Nice init.py right there, init.py?
@HiltonFernandes
@HiltonFernandes 5 месяцев назад
Great video ! Congrats !
@HiltonFernandes
@HiltonFernandes 5 месяцев назад
If you don't mind, it would be nice to have another video explaining the initialization that __init__.py can do.
@2MinutesPy
@2MinutesPy 5 месяцев назад
Thank you very much!
@2MinutesPy
@2MinutesPy 5 месяцев назад
Sure, very soon
@luuu_na35
@luuu_na35 8 месяцев назад
Great! I can make my own package now.
@2MinutesPy
@2MinutesPy 8 месяцев назад
Great!
@uuuummm9
@uuuummm9 6 месяцев назад
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.
@prof-caio-jannuzzi
@prof-caio-jannuzzi 8 месяцев назад
Que top! Curti esse canal
@2MinutesPy
@2MinutesPy 8 месяцев назад
Obrigado
@Arvine_Lyrics
@Arvine_Lyrics 6 месяцев назад
It's used mainly to prevent circular imports😊
@2MinutesPy
@2MinutesPy 6 месяцев назад
Nowadays, this file is not needed as Python is advancing but yeah, you can prevent circular imports within your projects using this file.
@ameyadali1768
@ameyadali1768 8 месяцев назад
Underrated
@dweepverma3662
@dweepverma3662 6 месяцев назад
Loved it
@2MinutesPy
@2MinutesPy 6 месяцев назад
Thanks
@freepythoncode
@freepythoncode 8 месяцев назад
Thank you so much 🙂❤
@2MinutesPy
@2MinutesPy 8 месяцев назад
Always welcome
@JohnintheTyranny
@JohnintheTyranny 2 месяца назад
Doesn't it do the same thing if you create a class in any other file?
@kerimalpalt
@kerimalpalt 9 месяцев назад
Because the person who wrote it is from South London
@emman100
@emman100 9 месяцев назад
🤣💀
@oliveselow-bw3py
@oliveselow-bw3py 8 месяцев назад
Like it..!
@2MinutesPy
@2MinutesPy 8 месяцев назад
Glad you like it!
@venil82
@venil82 7 месяцев назад
These double underlined magic methods is such a terrible design
@theginix
@theginix 3 месяца назад
Why?
@Jakub1989YTb
@Jakub1989YTb Месяц назад
Ever heard of namespaces?
@africknchickn_YT
@africknchickn_YT 7 месяцев назад
AI generated?
@khanbasharat
@khanbasharat 7 месяцев назад
New subscriber here!
@2MinutesPy
@2MinutesPy 7 месяцев назад
Welcome!!
@IcyyDicy
@IcyyDicy 8 месяцев назад
This feels AI generated The voice has weird intonation
@2MinutesPy
@2MinutesPy 8 месяцев назад
Sorry for inconvenience caused
@kuljeetkumar4657
@kuljeetkumar4657 6 месяцев назад
Content is top notch in 2 mins. Ads take 30 seconds. That is the only bad part.
@2MinutesPy
@2MinutesPy 6 месяцев назад
Thanks!
@kahvuongng2701
@kahvuongng2701 Месяц назад
ty
@2MinutesPy
@2MinutesPy Месяц назад
💛💛
@VahidOnTheMove
@VahidOnTheMove 9 месяцев назад
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 9 месяцев назад
Yeah right
@onepun9583
@onepun9583 7 месяцев назад
what's the full syntax to implement it?
@Thekingslayer-ig5se
@Thekingslayer-ig5se 4 месяца назад
Man your an icon
@flosrv3194
@flosrv3194 4 месяца назад
I always try it and nothing works for my projects
@2MinutesPy
@2MinutesPy 4 месяца назад
ohh
@forlorn4523
@forlorn4523 2 месяца назад
I am able import methods without creating the init file
@2MinutesPy
@2MinutesPy 2 месяца назад
Yes, you can in newer versions of Python. But not putting __init__.py file sometimes cause circular imports error within modules.
@RocketPropelledWombat
@RocketPropelledWombat 6 месяцев назад
The classics....
@vishalvishwakarma4389
@vishalvishwakarma4389 7 месяцев назад
This works even when there is no init.py file
@2MinutesPy
@2MinutesPy 7 месяцев назад
Yeah, if you have a Python version above 3.3
@vladhaidukkk-learning
@vladhaidukkk-learning 6 месяцев назад
As far as I know, this file became optional after Python 3.3
@2MinutesPy
@2MinutesPy 6 месяцев назад
Yeah
@onehotseat
@onehotseat 6 месяцев назад
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 6 месяцев назад
Noted and thanks
@udonwadon2045
@udonwadon2045 6 месяцев назад
the amount of quirks in python that is senseless makes me prefer ruby.
@AnweshAdhikari
@AnweshAdhikari 7 месяцев назад
@carlosmspk
@carlosmspk 6 месяцев назад
Except python no longer needs __init__.py and works perfectly fine without it. I was hoping this video would address this :(
@2MinutesPy
@2MinutesPy 6 месяцев назад
Yeah and sorry
@Carhill
@Carhill 8 месяцев назад
Narration is clearly text-to-speech, but the dialogue itself feels like it was spat out by a LLM. Awful.
@murphygreen8484
@murphygreen8484 7 месяцев назад
Isn't this no longer needed as of python 3.3?
@2MinutesPy
@2MinutesPy 7 месяцев назад
Yeah, you're right but a developer should be aware of this.
@kvnptl4400
@kvnptl4400 6 месяцев назад
Sometimes For me it's 1MinutePy
@user-uc6wo1lc7t
@user-uc6wo1lc7t 6 месяцев назад
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
@user-en7dl1lv6b
@user-en7dl1lv6b 6 месяцев назад
It’s a module
@HussinJaafari
@HussinJaafari 6 месяцев назад
Since 3.9, you no more need the __init__.py anymore
@2MinutesPy
@2MinutesPy 6 месяцев назад
Yeah
@fqidz
@fqidz 9 месяцев назад
i cant tell if the voice is ai or a real person
@alex11943
@alex11943 8 месяцев назад
1:11 is a dead giveaway that it's AI voice. But at least the explanation is good
@TeraBaap-pc1dd
@TeraBaap-pc1dd 9 месяцев назад
Susbscribed on 999 The next one would be 1k directly
@2MinutesPy
@2MinutesPy 9 месяцев назад
Thanks
@ifstatementifstatement2704
@ifstatementifstatement2704 8 месяцев назад
Haven’t used it yet but from what I’ve seen it seems to be a pretentious c++ header file wannabe.
@geoafrikana
@geoafrikana 7 месяцев назад
That escalated quickly
@s1mo
@s1mo 8 месяцев назад
bri'ish people when they write the __init__.py file
@royk7712
@royk7712 8 месяцев назад
why because the brit say it often like "init bruv?" jk
@tyrojames9937
@tyrojames9937 8 месяцев назад
😁😁
@gaot5281
@gaot5281 7 месяцев назад
If I put the file test.py in a folder it did not work. The error is "ModuleNotFoundError: No module named "my_package"
@mihirjog2707
@mihirjog2707 6 месяцев назад
That's useful, init?
@feazysil2707
@feazysil2707 7 месяцев назад
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 7 месяцев назад
Noted
@joseph2625
@joseph2625 7 месяцев назад
PEP420 was drafted over a decade ago. Are you just pretending that it doesn't exist?
@Parodyyy
@Parodyyy 4 месяца назад
What?
@zeez7777
@zeez7777 6 месяцев назад
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
@shubhamjha9082
@shubhamjha9082 6 месяцев назад
🙄 😅
@RyanWaite28
@RyanWaite28 8 месяцев назад
I like Node.JS better 🤷🏾‍♂️
@2MinutesPy
@2MinutesPy 8 месяцев назад
No worries, you can try Python also sometimes
@marcopulido3087
@marcopulido3087 7 месяцев назад
__init__.py is a kind of gluon 😂
@maudjito
@maudjito 6 месяцев назад
Hate the AI voice
@schuylergood4897
@schuylergood4897 8 месяцев назад
This looks AI generated
@pooria10001
@pooria10001 3 месяца назад
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 9 месяцев назад
Text to speech. Bleh
@susugar3338
@susugar3338 8 месяцев назад
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 8 месяцев назад
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.
@komodo-dragon
@komodo-dragon 6 месяцев назад
Bc python is british
@2MinutesPy
@2MinutesPy 6 месяцев назад
😁
Далее
Сказала дочке НЕТ!
00:24
Просмотров 1,1 млн
Bike Challenge
00:20
Просмотров 23 млн
Прохожу маску ЭМОЦИИ🙀 #юмор
00:59
Coding Was HARD Until I Learned These 5 Things...
8:34
How To Use List Comprehension In Python
6:41
Просмотров 8 тыс.
10 Important Python Concepts In 20 Minutes
18:49
Просмотров 92 тыс.
The most important Python script I ever wrote
19:58
Просмотров 193 тыс.
If __name__ == "__main__" for Python Developers
8:47
Просмотров 400 тыс.
15 Python Libraries You Should Know About
14:54
Просмотров 387 тыс.
What does Python's __init__ method do?
6:56
Просмотров 33 тыс.
Сказала дочке НЕТ!
00:24
Просмотров 1,1 млн