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.
__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, 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.
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 !
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.
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.
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.
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__
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
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.
__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.
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
explanation is clear concise to the point.. no redundancy. love it.
Thanks bruh
I can't thank you enough for this. This is the best explanation ever. Thank you very much
Glad it was helpful!
Absolutely hidden information, I couldn’t found out this in udemy courses. Thank you🎉
Glad it was helpful!
Omg what an amazing video to come across while in my first weeks of using python. 👍👍👍
Great to hear!
python is super cool
So it's basically Python's way of declaring namespaces with optional super constructor functionality. That's what I understood.
Great vid btw! :)
Exactly!
Also can help in abstraction and encapsulation(__all__) too
Greatly explained in the most simplistic way possible. Thanks
Glad it was helpful!
High clarity, Excellent Presentation and ultimate communication very interesting video. Thanks and Regards.
So nice of you
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.
This needs so much more engagement, also i might comment another just for engagement purposes
Thanks for support.
no. i want to gatekeep this
best one yet
Thanks, mate
Pretty sure this is an ai video
nah
Yep
It was still helpful
Great video, keep making new ones. The way you explain things in 2 minutes is amazing ! Great work !
Thanks a lot!
Thanks boss
More please
More
Sure brother
Wow new format for me, thanks
Glad to hear it!
Great startups! Keep on making more videos.
Thank you, I will
Great video, short and concise!
Much appreciated!
Python is great. Burning things is greater.
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.
Awesome video!
Thanks!
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
Thanks for your video!
My pleasure!
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
Thank you ❤
Always welcome
So surprisingly, its as same as index.ts or index.js in js packages.
I guess a tree structure diagram would help. But great video, straight to the point.
Thanks
Sir,
If possible Simultaneous video on SQL. Regards.
As soon as possible
Great! I can make my own package now.
Great!
Great video. I recommend making a playlist for easy access to all your related video.
Thank you, I will
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.
this video made me very clear about the python package concept with a practical example
Thanks
Nice init.py right there, init.py?
Que top! Curti esse canal
Obrigado
Thank you so much 🙂❤
Always welcome
I love this
Great...
Loved it
Thanks
nice lesson
Thanks! 😃
Underrated
Doesn't it do the same thing if you create a class in any other file?
what's the full syntax to implement it?
I always try it and nothing works for my projects
ohh
Like it..!
Glad you like it!
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.
New subscriber here!
Welcome!!
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.
These double underlined magic methods is such a terrible design
Why?
This works even when there is no init.py file
Yeah, if you have a Python version above 3.3
Ever heard of namespaces?
Because the person who wrote it is from South London
🤣💀
This feels AI generated
The voice has weird intonation
Sorry for inconvenience caused
Narration is clearly text-to-speech, but the dialogue itself feels like it was spat out by a LLM.
Awful.
Content is top notch in 2 mins. Ads take 30 seconds. That is the only bad part.
Thanks!
Isn't this no longer needed as of python 3.3?
Yeah, you're right but a developer should be aware of this.
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.
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
ty
💛💛
Man your an icon
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
AI generated?
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
Sometimes For me it's 1MinutePy
As far as I know, this file became optional after Python 3.3
Yeah
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
The classics....
❤
bri'ish people when they write the __init__.py file
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
this is not true, You can still import functions and classes without __init__.py
Yeah, because of namespace. This is about in the context of regular packages in Python
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.
Except python no longer needs __init__.py and works perfectly fine without it. I was hoping this video would address this :(
Yeah and sorry
It’s a module
Since 3.9, you no more need the __init__.py anymore
Yeah
Susbscribed on 999
The next one would be 1k directly
Thanks
😁😁
why because the brit say it often like "init bruv?"
jk
Haven’t used it yet but from what I’ve seen it seems to be a pretentious c++ header file wannabe.
That escalated quickly
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
🙄 😅
I like Node.JS better 🤷🏾♂️
No worries, you can try Python also sometimes
__init__.py is a kind of gluon 😂
PEP420 was drafted over a decade ago. Are you just pretending that it doesn't exist?
What?
Hate the AI voice
This looks AI generated
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
😁