Intermediate Python Tutorial: How to Use the __init__.py File
ฝัง
- เผยแพร่เมื่อ 21 ก.ย. 2024
- A succinct tutorial on how to use the __init__.py file to import directories as python libraries.
Welcome to my series for Learning Python, where I will be teaching fundamental concepts in the Python programming language.
These videos will focus on one aspect of Python at a time, but your understanding of the material will be greatly improved through usage. I recommend applying the concepts described here in your own project, or viewing the Developer Diary series where I use these tools on bigger projects.
• Developer Diary: Open ...
Hello everyone! Another user was having a problem that I want to share with you guys. When you import a package to python, the interpreter searches several paths to find your library - one of which being the working directory. If, however, one of those other paths has a library with the same name, that one will take precedence! If you create a library called Matplotlib, python will still import the original instead of yours!
I came here to learn __init__, but the way you have demonstrated is absolutely amazing; now I understand what from, import, and __init__.py do. Brilliant!.
Thank you! I appreciate it!
Same! Good job
Slight correction. I haven’t finished watching it yet. But if you do import resources.object_ones. You can do object_ones.Ones(). That’s it. It’s more to type, which is why the from is used. But you will often see packages like pandas like this. import pandas as pd. Then we do pd.DataFrame() blah blah.
Finally a short and to the point tutorial that actually tells me what the frick a __init__.py is and how to use it, thank you!
One of the best explanations I've heard on the subject. Thanks!
Jeeez I always used init files without typing in anything. 2 years passed here I am finding out this blessing of information. This will clean up a lot of mess in my projects. Huge thanks!!!!!
Thank you! The first two minutes explained my confusion about why some of my code would work without errors when other code wouldn't. Definitely helped me. Thanks!!!
That was what my problem
OH MY GOD it's so simple yet noone has ever explained it like this!!!
Concise, clear and very informative. Thank you ❤
concise, short, to the point, just what I was expecting
Short and straightforward, great work Eric!
randomly came across and whatever this is, this helped me understand soo much init and the concept of library, in such a short and engaging video. thank you so much.
You're a gem! Such a great teacher! If everyone taught like this anyone could learn Python in days!
This was the 'dark souls boss' of python for me. You executed him in under 6 minutes. Thank you sensei.
Extremely helpful and very well explained.
Great! I was reading tutorials about python to grasp everything. Not a great idea. It was confusing especially the __init__ but your videos makes it so easy to understand! Love it. Thank you so much
Thanks, no confusing words used. Clearly explained!
It takes an altogether different mind to even bother about this aspect!! Thanks.
Thank you for this. You're a good teacher and your explanations are clear.
I was struggling because all the answers were "just append the directory to the system path" or "put it in the init file" with no information on WHAT to put in the file.
this might be the best explanation about imports on youtube. thanks man
Thanks for that clear description. This make reusing code so much easier. I have been trying to figure this out for two weeks.
i dont see how this video could have been better. thank you
Thank you very much Eric, I sure appreciate you for this short informative video. Will support you till the end .
Much appreciated!
Short and straight to the point. I love this!
Thanks, Eric. This short video was just what I was looking for. I'm a Python newcomer and packages have been difficult to grok until now.
Excellent explanation, thank you
Superb succinct tutorial - many thanks!
Very good explanation. I was wondering why a lot of projects had hundreds of lines of code added to their __init__ file and this has helped a lot. Thanks mate!
Oh my lord my eyes!!! You are one of like three people I’ve ever met that works in a light theme….
Super helpful! I love how you walked through the logic
Waow, great explanation thank you so much. I was able to follow along :)
One of the better explanations i have seen on youtube!
Thank you!
subscriber number 382 here.
great content. to the point. thanks
Thanks for the sub, welcome aboard!
Explained very well 👏
Thanks You very much from France !
Thanks a lot Mr.Eric 😍😍
Definitely useful 👍👍
Wow how come this guy have very less subscribers. you deserve more.
You are very good at explaining things. Great video!
Thank you so much for this video. I find this topic relatively challenging to understand but now is completely clear for me. Thanks again.
Really good explanation Eric! you are really the first TH-camr that explained it so good that i can understand it right away without becoming confused xD. You got my sub :)
Thank you! Feel free to join our discord server if you have any questions! I'm happy to help :)
Very short and nice and clear explanation.
Thanks a lot mate, was trying for ages this.. but resolved now. Thank you .
Thanks , this video help me to get more clear and easily understanadable information
Awesome tutorial ........
Broo, you are a genius. Thank you for explaining it so clearly. Subbed and liked!
It helps understand the concept better!
Thanks for the explanation, it's now clearer
Thanks Mr.Eric. it's a good explanation... 🎉
Excellent! man, I will recommend your channel.
Very nice and clear tutorial, it helps a lot!
Congrats!! Very very well explained.
Thank you!
Short and to the point. Great work Eric 👌
I think I would be coming back to you the next time as well for any help on python.
Very useful and very clear. Thank you!
Clear and concise ... Good job!
Well done. Explained excellently.
Glad it was helpful!
very helpful thanks!
great explanation!
Simple but great work.. Thanks
Just what I was looking for!
Thanks for the help! Really good explanation
Glad you liked it, thank you!
Straight to the point, thanks!
Thanks a ton Eric!!
Thank you so much for this video! However, I was wondering if we could simply use "from resources import *" to avoid specifying which object we're trying to import?
Thanks for your help!
thanks man that was excellent, also: for python2 empty __init__.py files work wonders X-D
Thanks Brozeff!
Amazing! Thanks!
The title of this video should be: "how to make a tutorial that lets you quickly learn something useful" 🙂
Thanks for this. I really needed to understand this.
Glad it was helpful!
I really like how you smash the poor keyboard
This is really helpful. Thank you so much!
Awesome!
Great
Very beautifully explained brother
Nice video
Interesante. Gracias
oh my days thank you
Great🎉 .Crystal clear ❤
I think you can also write something like __add__ = ["object_one", "object_two"] inside of this __init__.py file.
I think this would be pretty complete if it had mentioned __all__
Nice Video Bro
thank you fore share it
Brilliant explanation! Thank you:)
I think you could call these two functions without assigning them to variables. You could write:
resources.One()
resources.Two()
Instead of:
o = resources.One()
t = resources.Two()
You will get the same result.
nice video
Another use of __init__.py file is to provide an interface so if you change structure of package you only need to change the init file and not other imports in your client code.
When i just joined a python project (it was Django based) i noticed those __init__ files almost in every directory. They also referred to each other in a quite chaotic way.
Few years later i started working on my own projects (this time fastapi) and the things do not seem to be clear still. I noticed very weird things - e.g. my main file may work fine, but when i run my tests (which use the main file) i get errors "package 'xyz' is not found". It looks like there is a lot of magic in terms of visibility of packages. Sometimes they can see parent packages, sometimes cannot. Thee is also that feature of relative import which also behaves not as expected sometimes.
So yeah i have difficult times understanding the idea nd how to structure my projects.
Fantastic explanation!
your sudden appereance in last scared me because your voice changed suddenly.
Just curious, what does a blank __init__.py file do in this case? Until this video, everywhere I looked was saying "oh just create an empty __init__.py file and it should work", guess what, it never worked. Thanks for the video, that was very helpful.
ok but what if tutorial.py has to import from a ../ dir?
Honestly blows my mind how a language like python could have designed their module/package system so incompetently.
Python's best quality is that it is easy to learn. It opens the door to most programming, but is usually the worst option for a given project.
Thank you very much
thank youfor this.
That was straight up to the point. Can you make more vids on tricky parts of python? Some concepts might be challenging to wrap your head around, especially from someone coming from other languages.
Thank you, I try to make videos like these as the topic comes up in my daily workflow. If you point something out to me, though, I'd be happy to make a video on it.
@@eom-dev I have some topics in mind. How to structure a python code since, I have noticed that untyped languages are not ideal for big projects. And another topic that would have been sweet is software development architecture styles. Those two topics always come up together.
Now everything just makes sense 😁
Thanks Eric, this was helpful
You're welcome, glad it helped!
So what is the purpose of having an empty __init__.py file? I've seen this in many projects...
Fun tutorial! The use of relative paths in __init__.py works, too:
from .object_one import One # relative path same as resources.object_one import One ...
from .object_two import Two # relative path same as resources.object_two import Two...
In my use case it actually only worked using relative paths. Thank you tho
Why do you have to write “from resources.object_one import One” in the init file when it’s already inside of the resources folder. I would’ve thought the line should be just “from object_one import One” 🤔. Is this something to do with where the cwd is?
for this to work, do I need to hit the keyboard as hard as you?