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 ...

ความคิดเห็น • 194

  • @eom-dev
    @eom-dev  3 ปีที่แล้ว +33

    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!

  • @ajayjoseph4498
    @ajayjoseph4498 3 ปีที่แล้ว +97

    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!.

    • @eom-dev
      @eom-dev  3 ปีที่แล้ว +5

      Thank you! I appreciate it!

    • @PatrickSteil
      @PatrickSteil ปีที่แล้ว +2

      Same! Good job

    • @MarkSmith-vo1vn
      @MarkSmith-vo1vn 2 หลายเดือนก่อน

      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.

  • @jackcaesar2596
    @jackcaesar2596 3 ปีที่แล้ว +30

    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!

  • @mookjais
    @mookjais 9 วันที่ผ่านมา

    One of the best explanations I've heard on the subject. Thanks!

  • @mihailb0r0koko89
    @mihailb0r0koko89 2 ปีที่แล้ว +1

    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!!!!!

  • @davidnobles162
    @davidnobles162 ปีที่แล้ว +2

    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!!!

  • @devdrane
    @devdrane หลายเดือนก่อน +1

    OH MY GOD it's so simple yet noone has ever explained it like this!!!

  • @parralelife
    @parralelife 2 หลายเดือนก่อน

    Concise, clear and very informative. Thank you ❤

  • @utpalmohane1416
    @utpalmohane1416 ปีที่แล้ว +1

    concise, short, to the point, just what I was expecting

  • @samarjitdebnath5996
    @samarjitdebnath5996 2 ปีที่แล้ว +7

    Short and straightforward, great work Eric!

  • @cysong88161
    @cysong88161 3 หลายเดือนก่อน

    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.

  • @JV-jc7ci
    @JV-jc7ci ปีที่แล้ว +1

    You're a gem! Such a great teacher! If everyone taught like this anyone could learn Python in days!

  • @c0nsumption
    @c0nsumption 11 หลายเดือนก่อน

    This was the 'dark souls boss' of python for me. You executed him in under 6 minutes. Thank you sensei.

  • @nathanielhorn144
    @nathanielhorn144 5 หลายเดือนก่อน

    Extremely helpful and very well explained.

  • @ahmedalhamadah5999
    @ahmedalhamadah5999 7 หลายเดือนก่อน

    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

  • @swarnimjambhule3264
    @swarnimjambhule3264 2 หลายเดือนก่อน

    Thanks, no confusing words used. Clearly explained!

  • @adityanjsg99
    @adityanjsg99 2 ปีที่แล้ว

    It takes an altogether different mind to even bother about this aspect!! Thanks.

  • @JustPlainRob
    @JustPlainRob 9 หลายเดือนก่อน

    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.

  • @splendorman7922
    @splendorman7922 2 ปีที่แล้ว

    this might be the best explanation about imports on youtube. thanks man

  • @derekknowlton2384
    @derekknowlton2384 2 ปีที่แล้ว +1

    Thanks for that clear description. This make reusing code so much easier. I have been trying to figure this out for two weeks.

  • @adrianmisak07
    @adrianmisak07 2 ปีที่แล้ว

    i dont see how this video could have been better. thank you

  • @alkhashtee
    @alkhashtee 3 ปีที่แล้ว +2

    Thank you very much Eric, I sure appreciate you for this short informative video. Will support you till the end .

    • @eom-dev
      @eom-dev  3 ปีที่แล้ว +2

      Much appreciated!

  • @Jerry-uc1pn
    @Jerry-uc1pn ปีที่แล้ว

    Short and straight to the point. I love this!

  • @mrc1500
    @mrc1500 2 ปีที่แล้ว

    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.

  • @kirito3082
    @kirito3082 7 หลายเดือนก่อน

    Excellent explanation, thank you

  • @CyberPsyLen
    @CyberPsyLen 2 ปีที่แล้ว +1

    Superb succinct tutorial - many thanks!

  • @dponcelas
    @dponcelas ปีที่แล้ว

    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!

  • @andrewiglinski148
    @andrewiglinski148 ปีที่แล้ว

    Oh my lord my eyes!!! You are one of like three people I’ve ever met that works in a light theme….

  • @RebeccaZieber
    @RebeccaZieber 8 หลายเดือนก่อน

    Super helpful! I love how you walked through the logic

  • @christofferd647
    @christofferd647 ปีที่แล้ว

    Waow, great explanation thank you so much. I was able to follow along :)

  • @elias043011
    @elias043011 3 ปีที่แล้ว

    One of the better explanations i have seen on youtube!

    • @eom-dev
      @eom-dev  3 ปีที่แล้ว

      Thank you!

  • @RealisticSohaib
    @RealisticSohaib 3 ปีที่แล้ว +1

    subscriber number 382 here.
    great content. to the point. thanks

    • @eom-dev
      @eom-dev  3 ปีที่แล้ว +1

      Thanks for the sub, welcome aboard!

  • @MuhammadAbdullahTariq-iz4ij
    @MuhammadAbdullahTariq-iz4ij 9 หลายเดือนก่อน

    Explained very well 👏

  • @oursblanc6477
    @oursblanc6477 2 ปีที่แล้ว

    Thanks You very much from France !

  • @nyanlinnhtet7773
    @nyanlinnhtet7773 ปีที่แล้ว

    Thanks a lot Mr.Eric 😍😍

  • @rutvijgajjar5172
    @rutvijgajjar5172 5 วันที่ผ่านมา

    Definitely useful 👍👍

  • @girishdoddi4831
    @girishdoddi4831 2 ปีที่แล้ว

    Wow how come this guy have very less subscribers. you deserve more.

  • @cusematt23
    @cusematt23 9 หลายเดือนก่อน

    You are very good at explaining things. Great video!

  • @armandoWebDev
    @armandoWebDev 2 ปีที่แล้ว

    Thank you so much for this video. I find this topic relatively challenging to understand but now is completely clear for me. Thanks again.

  • @jonnynilsen9276
    @jonnynilsen9276 3 ปีที่แล้ว +1

    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 :)

    • @eom-dev
      @eom-dev  3 ปีที่แล้ว

      Thank you! Feel free to join our discord server if you have any questions! I'm happy to help :)

  • @sumangupta871
    @sumangupta871 7 หลายเดือนก่อน

    Very short and nice and clear explanation.

  • @Vixnuverse
    @Vixnuverse ปีที่แล้ว

    Thanks a lot mate, was trying for ages this.. but resolved now. Thank you .

  • @himanshuyadav1712
    @himanshuyadav1712 ปีที่แล้ว

    Thanks , this video help me to get more clear and easily understanadable information

  • @rubyashby8303
    @rubyashby8303 ปีที่แล้ว

    Awesome tutorial ........

  • @junealexissantos4341
    @junealexissantos4341 ปีที่แล้ว

    Broo, you are a genius. Thank you for explaining it so clearly. Subbed and liked!

  • @senthilnayagan7047
    @senthilnayagan7047 ปีที่แล้ว

    It helps understand the concept better!

  • @eddyeffy
    @eddyeffy 2 ปีที่แล้ว

    Thanks for the explanation, it's now clearer

  • @arunkumaranbalagan6048
    @arunkumaranbalagan6048 ปีที่แล้ว

    Thanks Mr.Eric. it's a good explanation... 🎉

  • @gpet2184
    @gpet2184 2 ปีที่แล้ว

    Excellent! man, I will recommend your channel.

  • @chianlee1381
    @chianlee1381 3 ปีที่แล้ว +2

    Very nice and clear tutorial, it helps a lot!

  • @darioBenedict
    @darioBenedict 3 ปีที่แล้ว +1

    Congrats!! Very very well explained.

    • @eom-dev
      @eom-dev  3 ปีที่แล้ว

      Thank you!

  • @kya-baat
    @kya-baat ปีที่แล้ว

    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.

  • @giacomodavide3316
    @giacomodavide3316 9 หลายเดือนก่อน

    Very useful and very clear. Thank you!

  • @bulldawg4498
    @bulldawg4498 ปีที่แล้ว

    Clear and concise ... Good job!

  • @ahobbo6401
    @ahobbo6401 2 ปีที่แล้ว +1

    Well done. Explained excellently.

    • @eom-dev
      @eom-dev  2 ปีที่แล้ว

      Glad it was helpful!

  • @saullandiof5768
    @saullandiof5768 9 วันที่ผ่านมา

    very helpful thanks!

  • @avnichakraverty7783
    @avnichakraverty7783 ปีที่แล้ว

    great explanation!

  • @sivanesans2424
    @sivanesans2424 2 ปีที่แล้ว

    Simple but great work.. Thanks

  • @t-distributedkid3825
    @t-distributedkid3825 ปีที่แล้ว

    Just what I was looking for!

  • @the12nina
    @the12nina 3 ปีที่แล้ว +1

    Thanks for the help! Really good explanation

    • @eom-dev
      @eom-dev  3 ปีที่แล้ว

      Glad you liked it, thank you!

  • @Ma1ne2
    @Ma1ne2 ปีที่แล้ว

    Straight to the point, thanks!

  • @dhirajkumarsahu999
    @dhirajkumarsahu999 7 หลายเดือนก่อน

    Thanks a ton Eric!!

  • @TomWelch-l5q
    @TomWelch-l5q หลายเดือนก่อน

    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!

  • @boringsoftware2093
    @boringsoftware2093 7 หลายเดือนก่อน

    thanks man that was excellent, also: for python2 empty __init__.py files work wonders X-D

  • @pascal831
    @pascal831 ปีที่แล้ว

    Thanks Brozeff!

  • @inteligenciaartificiuau
    @inteligenciaartificiuau 7 หลายเดือนก่อน

    Amazing! Thanks!

  • @StefanoVerugi
    @StefanoVerugi ปีที่แล้ว

    The title of this video should be: "how to make a tutorial that lets you quickly learn something useful" 🙂

  • @fhuadbalogun2997
    @fhuadbalogun2997 3 ปีที่แล้ว

    Thanks for this. I really needed to understand this.

    • @eom-dev
      @eom-dev  3 ปีที่แล้ว

      Glad it was helpful!

  • @schlaus_kwab
    @schlaus_kwab 2 ปีที่แล้ว

    I really like how you smash the poor keyboard

  • @nikhil182
    @nikhil182 2 ปีที่แล้ว

    This is really helpful. Thank you so much!

  • @АлександрМилко-я1с
    @АлександрМилко-я1с 5 หลายเดือนก่อน

    Awesome!

  • @srh1034
    @srh1034 5 วันที่ผ่านมา

    Great

  • @nadeemNadamat
    @nadeemNadamat 2 ปีที่แล้ว

    Very beautifully explained brother

  • @amit12000
    @amit12000 2 ปีที่แล้ว

    Nice video

  • @rogerayalarojas4617
    @rogerayalarojas4617 7 หลายเดือนก่อน

    Interesante. Gracias

  • @drover7476
    @drover7476 ปีที่แล้ว

    oh my days thank you

  • @jsj757
    @jsj757 11 หลายเดือนก่อน

    Great🎉 .Crystal clear ❤

  • @airatvaliullin8420
    @airatvaliullin8420 ปีที่แล้ว +1

    I think you can also write something like __add__ = ["object_one", "object_two"] inside of this __init__.py file.

  • @cm_lolsborn
    @cm_lolsborn 6 วันที่ผ่านมา

    I think this would be pretty complete if it had mentioned __all__

  • @rubenschulte1299
    @rubenschulte1299 2 ปีที่แล้ว

    Nice Video Bro

  • @Airoupmusic
    @Airoupmusic 10 หลายเดือนก่อน

    thank you fore share it

  • @maksymilianbiruk6877
    @maksymilianbiruk6877 2 ปีที่แล้ว

    Brilliant explanation! Thank you:)

  • @HAMHAMAT
    @HAMHAMAT ปีที่แล้ว

    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.

  • @hasanshovon3402
    @hasanshovon3402 3 หลายเดือนก่อน

    nice video

  • @durgeshkshirsagar116
    @durgeshkshirsagar116 ปีที่แล้ว +1

    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.

  • @uuuummm9
    @uuuummm9 10 หลายเดือนก่อน

    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.

  • @patrickkrenz515
    @patrickkrenz515 ปีที่แล้ว

    Fantastic explanation!

  • @MukeshYadav-wb5uo
    @MukeshYadav-wb5uo ปีที่แล้ว

    your sudden appereance in last scared me because your voice changed suddenly.

  • @rootifera
    @rootifera 8 หลายเดือนก่อน

    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.

  • @PhilsArtVibes
    @PhilsArtVibes 7 หลายเดือนก่อน +1

    ok but what if tutorial.py has to import from a ../ dir?

  • @valizeth4073
    @valizeth4073 2 ปีที่แล้ว +1

    Honestly blows my mind how a language like python could have designed their module/package system so incompetently.

    • @eom-dev
      @eom-dev  2 ปีที่แล้ว

      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.

  • @kmehour
    @kmehour 11 หลายเดือนก่อน

    Thank you very much

  • @JeffreyMcintyre-b2j
    @JeffreyMcintyre-b2j ปีที่แล้ว

    thank youfor this.

  • @kalekber
    @kalekber 3 ปีที่แล้ว

    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.

    • @eom-dev
      @eom-dev  3 ปีที่แล้ว

      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.

    • @kalekber
      @kalekber 3 ปีที่แล้ว +2

      ​@@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.

  • @toxxmoloi1873
    @toxxmoloi1873 2 ปีที่แล้ว

    Now everything just makes sense 😁

  • @adhikarikalyan
    @adhikarikalyan 3 ปีที่แล้ว

    Thanks Eric, this was helpful

    • @eom-dev
      @eom-dev  3 ปีที่แล้ว

      You're welcome, glad it helped!

  • @jdiegosf
    @jdiegosf 6 หลายเดือนก่อน

    So what is the purpose of having an empty __init__.py file? I've seen this in many projects...

  • @johnrm9
    @johnrm9 2 ปีที่แล้ว +2

    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...

    • @felipehonoratodesousa7835
      @felipehonoratodesousa7835 2 ปีที่แล้ว

      In my use case it actually only worked using relative paths. Thank you tho

  • @WondrousHello
    @WondrousHello ปีที่แล้ว

    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?

  • @chrisnorthall8317
    @chrisnorthall8317 8 หลายเดือนก่อน

    for this to work, do I need to hit the keyboard as hard as you?