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.
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.
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.
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.
__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.
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 !
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.
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.
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 😂)
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.
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.
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
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__
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.
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.
I love finding hidden gems like these on TH-cam
Thanks
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.
I must appreciate, that you took the time to explain handling imports within a package.
I just had a 30-minute chat with GPT by pasting your comment and "huh?" --learning about asyncio.run(()) and namespace, now, haha.
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.
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.
I can't thank you enough for this. This is the best explanation ever. Thank you very much
Glad it was helpful!
explanation is clear concise to the point.. no redundancy. love it.
Thanks bruh
We're __init__ to win-it!
for some reason this showed up in my feed, good stuff my guy. keep it up
Appreciate it
I just found your channel from the TH-cam recommendations. The 2 minute videos are a great idea!
Thank you!
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?
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.
@@2MinutesPy yes i also tried
__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.
Yeah, you can use directory-level import also to access say_hello function.
Absolutely hidden information, I couldn’t found out this in udemy courses. Thank you🎉
Glad it was helpful!
Pretty sure this is an ai video
nah
Yep
It was still helpful
Omg what an amazing video to come across while in my first weeks of using python. 👍👍👍
Great to hear!
python is super cool
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 !
Glad you liked it and thanks for valuable suggestion.
facts
Nah. If he does that he can fall for the temptation to include filling material.
Greatly explained in the most simplistic way possible. Thanks
Glad it was helpful!
So it's basically Python's way of declaring namespaces with optional super constructor functionality. That's what I understood.
Great vid btw! :)
Exactly!
High clarity, Excellent Presentation and ultimate communication very interesting video. Thanks and Regards.
So nice of you
This needs so much more engagement, also i might comment another just for engagement purposes
Thanks for support.
no. i want to gatekeep this
Thanks boss
More please
More
Sure brother
I guess a tree structure diagram would help. But great video, straight to the point.
Thanks
Also can help in abstraction and encapsulation(__all__) too
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.
Thanks
Great video, keep making new ones. The way you explain things in 2 minutes is amazing ! Great work !
Thanks a lot!
best one yet
Thanks, mate
Great startups! Keep on making more videos.
Thank you, I will
Wow new format for me, thanks
Glad to hear it!
So surprisingly, its as same as index.ts or index.js in js packages.
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.
Python is great. Burning things is greater.
Great video, short and concise!
Much appreciated!
Great video. I recommend making a playlist for easy access to all your related video.
Thank you, I will
Thanks for your video!
My pleasure!
Awesome video!
Thanks!
Doesn't it do the same thing if you create a class in any other file?
Sir,
If possible Simultaneous video on SQL. Regards.
As soon as possible
Nice init.py right there, init.py?
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 😂)
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.
this video made me very clear about the python package concept with a practical example
Thanks
nice lesson
Thanks! 😃
Great video !
Congrats !
If you don't mind, it would be nice to have another video explaining the initialization that __init__.py can do.
Thank you very much!
Sure, very soon
Great! I can make my own package now.
Great!
Thank you so much 🙂❤
Always welcome
Que top! Curti esse canal
Obrigado
These double underlined magic methods is such a terrible design
Why?
It's used mainly to prevent circular imports😊
Nowadays, this file is not needed as Python is advancing but yeah, you can prevent circular imports within your projects using this file.
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.
This works even when there is no init.py file
Yeah, if you have a Python version above 3.3
Loved it
Thanks
Because the person who wrote it is from South London
🤣💀
Narration is clearly text-to-speech, but the dialogue itself feels like it was spat out by a LLM.
Awful.
This feels AI generated
The voice has weird intonation
Sorry for inconvenience caused
I love this
Great...
what's the full syntax to implement it?
I always try it and nothing works for my projects
ohh
Content is top notch in 2 mins. Ads take 30 seconds. That is the only bad part.
Thanks!
Underrated
Like it..!
Glad you like it!
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
As far as I know, this file became optional after Python 3.3
Yeah
this is not true, You can still import functions and classes without __init__.py
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".
Noted and thanks
Isn't this no longer needed as of python 3.3?
Yeah, you're right but a developer should be aware of this.
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__
Yeah right
Except python no longer needs __init__.py and works perfectly fine without it. I was hoping this video would address this :(
Yeah and sorry
I am able import methods without creating the init file
Yes, you can in newer versions of Python. But not putting __init__.py file sometimes cause circular imports error within modules.
Since 3.9, you no more need the __init__.py anymore
Yeah
i cant tell if the voice is ai or a real person
1:11 is a dead giveaway that it's AI voice. But at least the explanation is good
Ever heard of namespaces?
Sometimes For me it's 1MinutePy
If I put the file test.py in a folder it did not work. The error is "ModuleNotFoundError: No module named "my_package"
the amount of quirks in python that is senseless makes me prefer ruby.
The classics....
AI generated?
bri'ish people when they write the __init__.py file
❤
It’s a module
Haven’t used it yet but from what I’ve seen it seems to be a pretentious c++ header file wannabe.
That escalated quickly
Man your an icon
New subscriber here!
Welcome!!
why because the brit say it often like "init bruv?"
jk
That's useful, init?
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
Susbscribed on 999
The next one would be 1k directly
Thanks
ty
💛💛
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
Noted
PEP420 was drafted over a decade ago. Are you just pretending that it doesn't exist?
What?
I like Node.JS better 🤷🏾♂️
No worries, you can try Python also sometimes
😁😁
This looks AI generated
__init__.py is a kind of gluon 😂
🙄 😅
Hate the AI voice
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.
Text to speech. Bleh
It's not a big problem, man. At least it's easy for my bad English to understand more than native English voice.
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.
Bc python is british
😁