Intermediate Python Tutorial: How to Use the __init__.py File

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ธ.ค. 2024

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

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

    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!

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

    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!

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

    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 5 หลายเดือนก่อน

      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.

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

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

    • @Md.OmarFaruk-t7f
      @Md.OmarFaruk-t7f 3 หลายเดือนก่อน

      That was what my problem

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

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

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

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

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

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

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

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

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

    Short and straightforward, great work Eric!

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

    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

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

    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.

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

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

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

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

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

    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.

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

    One of the better explanations i have seen on youtube!

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

      Thank you!

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

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

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

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

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

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

    Short and straight to the point. I love this!

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

    Thanks, no confusing words used. Clearly explained!

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

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

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

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

    Super helpful! I love how you walked through the logic

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

    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!

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

    Concise, clear and very informative. Thank you ❤

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

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

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

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

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

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

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

    Superb succinct tutorial - many thanks!

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

    You are very good at explaining things. Great video!

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

    Extremely helpful and very well explained.

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

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

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

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

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

    5:25 is there another solution to copy and paste the resources? For example, i want to share common resources among many projects, and I dont want to have to maintain copies. Thank you!

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

    Hey, I am really struggling with relative imports. It would be great if you make a vid about it

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

    can you please put library and code to different folders and access library. May be at the same level.

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

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

  • @olekristianmller-hansen4220
    @olekristianmller-hansen4220 11 หลายเดือนก่อน

    Hi! Can I loop through files and import them at run time? I don't know which files to import beforehand.

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

    what if i have 2 directories and i want to import class from from dir1 into my file which is in dir2
    Both dir in same path.

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

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

    Excellent! man, I will recommend your channel.

  • @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?

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

    What I missing here, resources.one() throwing me an error bcz i am bypassing object_one file. when I do import resources.object_one as one and then o = one.One() works fine

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

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

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

    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.

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

    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.

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

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

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

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

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

    If the __init__.py is empty in this case, the script fails. Why so ? Has class something to do with it ?

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

    Just what I was looking for!

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

    yeah but with filling init file with import statements, we just made imported modules depend on each other. so in this case object_one module now depends on object_two module, no? if I wanted to import object_one module, it will automatically try to load object_two module also. how do we avoid this completely? it is driving me crazy sometimes

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

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

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

    Very short and nice and clear explanation.

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

    Clear and concise ... Good job!

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

    It helps understand the concept better!

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

    I understood this concept now, One favor I want,
    How do we manage the different configurations(DevelopentConfig, QAConfig, ProductionConfig),
    They hold the variables that are used in all over the project, is there any way that we can mention
    if we want to use DevelopmentConfig or ProductionConfig without changing lots of code?

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

    Congrats!! Very very well explained.

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

      Thank you!

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

    If tutorial.py present in another directory . How can we import resources

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

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

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

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

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

    Hi Eric, what if the modules you want to import are from a different directory (non-parental relationship)?

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

      You would either create another _init__.py or move the files in such a way that you can call them. Think if the libraries as if they are in a file structure. As long as the Python_path has the folders in the parent path then you can call them. This is about not having to recall functions over and over again and being able to utilize one file to establish everything in a collective state.,

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

    Straight to the point, thanks!

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

    I need help. I am making an app and I need to import modules from a different folder and I can't get any solutions on internet. Can I just build one __init__.py file in the folder and will it work just like this as I don't want to involve the IDLE in this case is it possible only through script editor??

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

      Typically, using full paths will be helpful here. ~/project/import is more likely to cause problems than /home/me/project/import. That being said, it may be wise to package all of these imports together into a single environment directory that will contain everything needed for the project.

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

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

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

    I don't know what's the problem
    but mine is not working , I copied the same code but at the end compiler says
    from abc.one import One
    ModuleNotFoundError: No module named 'abc.one'; 'abc' is not a package

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

      Do you mind telling me what your directory structure is? If you want to jump on to the discord it would be easier to help you debug.
      discord.gg/PTWecyAbDU

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

    Very nice and clear tutorial, it helps a lot!

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

    Thanks! Understood it well and clear. The naming __init__.py is by convention, right?

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

    Very useful and very clear. Thank you!

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

    Is it possible to write conditional import statements in __init__.py ? i mean, If I have a package with 50 files and I want to use 10 among them in one case and in another case, I want to use 30 among them.

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

    Thanks a lot Mr.Eric 😍😍

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

    Thanks for the explanation, it's now clearer

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

    Well done. Explained excellently.

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

      Glad it was helpful!

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

    Definitely useful 👍👍

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

    Man it aint workin for me, for some reason it just say "No module named 'my_module' "

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

    Awesome tutorial ........

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

    Thanks You very much from France !

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

    Nice video though I think there might be a possible issue with using an __init__ file and I may be wrong however I can't seem to find anything on this.
    Hypothetically speaking you could have all your [from foo import bar's] in an __init__ file but can't this make circular imports?
    You have four files, A B, C and D each with their import in the __init__ (from A import a etc)
    A needs B
    B needs C
    C needs the other 2
    Doesn't this mean a Circular import will happen due to B getting C and C getting B due to the __init__ file as all imports from the __init__ file are "pushed" into each document that calls it

  • @MuhammadAbdullahTariq-iz4ij
    @MuhammadAbdullahTariq-iz4ij ปีที่แล้ว

    Explained very well 👏

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

    Great🎉 .Crystal clear ❤

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

    Very beautifully explained brother

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

    Thanks for the help! Really good explanation

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

      Glad you liked it, thank you!

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

    Excellent explanation, thank you

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

    Simple but great work.. Thanks

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

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

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

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

    Fantastic explanation!

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

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

    great explanation!

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

    Thanks for this. I really needed to understand this.

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

      Glad it was helpful!

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

    This is really helpful. Thank you so much!

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

    Thanks a ton Eric!!

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

    Amazing! Thanks!

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

    Brilliant explanation! Thank you:)

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

    why didn't you use __all__

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

    was awesome, cheers!

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

      Glad you enjoyed it!

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

    very helpful thanks!