What does '__init__.py' do in Python?

Поделиться
HTML-код
  • Опубликовано: 16 янв 2025

Комментарии • 96

  • @umka7536
    @umka7536 3 месяца назад +45

    Thanks for covering the role of __init__.py in the import of a module. Because a lot of video tutorials skip this information.

  • @arkie87
    @arkie87 3 месяца назад +14

    This is a great start on this topic. There arent actually a lot of youtube videos about this topic, and its hard to find information. Would appreciate a follow up that went more into detail, if you can. For instance: what exactly happens when you import things in the __init__.py file. What sort of start up logic do you need to do. What is the appropriate use of this file etc....

  • @dragweb7725
    @dragweb7725 3 месяца назад +54

    Thanks for this basic tutorial about imports, could it be possible to make a video about imports between modules in the same package ? cause this is always a pain in the a** to know how to do these correctly and everyone seems to use different tricks when searching for solutions on the net. That would be a really helpful video

    • @jokmenen_
      @jokmenen_ 3 месяца назад +1

      Same. Had to use sys.path to import packages from another folder yesterday. My brain exploded

    • @ego-lay_atman-bay
      @ego-lay_atman-bay 3 месяца назад +2

      I don't understand how it's difficult. If you have an __init__?py file in each folder, they become submodules, which you can import the same way as a file inside a package. If you need to import from one submodule to another, you use two dots instead of one.

    • @dragweb7725
      @dragweb7725 3 месяца назад +4

      @@ego-lay_atman-bay I'm not talking about imports outside a package (involving __init__.py), but more about imports between modules in the same package (same folder), as relative imports easily gives you a "attempted relative import but no parent package found" even with __init__.py everywhere, and direct import like "from module import func" without a dot it easily gives you a "no module named 'module' found"

    • @ego-lay_atman-bay
      @ego-lay_atman-bay 3 месяца назад +1

      @@dragweb7725 and what is the use case for such a project with that kind of folder structure?

  • @BohonChina
    @BohonChina 3 месяца назад +134

    you should mention the difference between having _init_.py and not having one in your module directory

    • @luisdominguesforprogramaca3221
      @luisdominguesforprogramaca3221 3 месяца назад +37

      He explained it's the _init_.py that makes the directory module a package and how to use that file to define the behaviour of the imports.

    • @BohonChina
      @BohonChina 3 месяца назад

      @@luisdominguesforprogramaca3221 NO _init_.py, INSERT path in PYTHONPATH, import module statement after inserting path will behave like a package too.

    • @DhavalAhir10
      @DhavalAhir10 3 месяца назад +26

      When you have `__init__.py` file in your folder directory, your folder called as "package".
      When you don't have `__init__.py` file in your folder directory, your folder still count as package but now your folder called as "Namespace Package" instead of "package".

    • @creed404yt9
      @creed404yt9 3 месяца назад +6

      Rewatch the video

    • @Omsip123
      @Omsip123 3 месяца назад +7

      How about explaining the difference in your comment?

  • @petsandpaws8906
    @petsandpaws8906 3 месяца назад +2

    Crystal clear. Thank you for explaining it very simpel!

  • @danialothman
    @danialothman 3 месяца назад +8

    that one class I ignored, it was this one in 2021. i got it now 😂 thanks for this!

  • @mikecanaday4888
    @mikecanaday4888 3 месяца назад +1

    Good job! Pycharm was a nice touch since that’s the environment that many use.

  • @lisaw7074
    @lisaw7074 Месяц назад

    This is so helpful and clearly explained! Thank you very much!

  • @thefanboy3285
    @thefanboy3285 2 месяца назад +1

    Thanks. Mind refresher, Life saver.

  • @laitei-e40
    @laitei-e40 3 месяца назад +2

    Great! Loved it, my hero 🎉

  • @Wh-al-es
    @Wh-al-es 2 месяца назад +1

    You're awesome.
    This one has a lot of great tips

  • @mukisajohnmary
    @mukisajohnmary 3 месяца назад +2

    Wonderful explanation. Thanks 🙏

  • @nobody2937
    @nobody2937 3 месяца назад

    Thank you indeed... Extremely clear, concise... Thank you ...

  • @michaelangellotti5741
    @michaelangellotti5741 3 месяца назад +2

    Quick and accurate. Thanks.

  • @onlysin530
    @onlysin530 2 месяца назад

    thank you very much! this also explains why some packages won't work when you just use import *... the __init__ flie in the package didn't explicitly declare the file that was needed! It is always a safer way to explicitly declaring the packages that one need then

  • @average_paniker
    @average_paniker 3 месяца назад +1

    Clearly explained. Thank you!

  • @ml_with_pranay
    @ml_with_pranay 3 месяца назад

    I enjoy your videos a lot! Keep posting😊

  • @awetinoawets9290
    @awetinoawets9290 3 месяца назад +4

    Very useful 🙏🙏🙏

  • @EquiliMario
    @EquiliMario 2 месяца назад

    The __all__ dunder method is something i didnt know yet. Neat

  • @GregMeece
    @GregMeece 10 дней назад

    FYI, from what I can tell, VS Code doesn't change the directory icon when you declare it to be a module (by adding dunder init . py).

  • @tejasmarvel2023
    @tejasmarvel2023 2 месяца назад

    very clear video. thanks

  • @purplemonkeydishwasher5269
    @purplemonkeydishwasher5269 2 месяца назад

    This is amazing. Im completely rebuilding an app Im working on based on this!!!

  • @FighterAceee94
    @FighterAceee94 3 месяца назад +2

    It would have been interesting to see the behavior of __init__.py when importing the same package from multiple different modules within your project. If I'm not mistaken, the __init__.py will be called only once (the first time any import statement is used).

  • @banjijohn
    @banjijohn 2 месяца назад

    ❤ thank you 🎉

  • @AdrianCortes-b7f
    @AdrianCortes-b7f 3 месяца назад

    Veryw ell explained, Thank you!

  • @basilchinedu144
    @basilchinedu144 3 месяца назад

    Thanks a lot, very helpful.

  • @davidlu1003
    @davidlu1003 3 месяца назад

    I love you, great videos, and please keep going.😁😁😁

  • @elastvd7503
    @elastvd7503 2 месяца назад

    Super , thanks

  • @giocic94
    @giocic94 3 месяца назад +7

    I use "__init__.py" also for importing all the libraries that I will import in the submodules. And then, in the submodules, I use "from . import ". I don't know if it's useful, I wanted to have organized imports.. Does it make sense?

  • @HiltonFernandes
    @HiltonFernandes 3 месяца назад

    Great video. Congrats !

  • @odevperovano
    @odevperovano 2 месяца назад

    Excellent, thankyou!

  • @HoSza1
    @HoSza1 3 месяца назад +3

    so a package is a container that contains an __init__ in it! 😂

  • @7th_dwarf542
    @7th_dwarf542 3 месяца назад

    brilliant! 👏

  • @AlWardani-k2h
    @AlWardani-k2h 3 месяца назад

    ❤❤❤❤❤ awesome

  • @goobiie
    @goobiie 3 месяца назад

    Very useful video

  • @_Garm_
    @_Garm_ 3 месяца назад

    aveasome thank you, would like to learn more about it :D

  • @benmatt8940
    @benmatt8940 3 месяца назад

    Utterly usefull

  • @michaelrstudley
    @michaelrstudley 3 месяца назад

    Fantastic video. How do you feel about placing all imports needed for the files inside?
    For example...
    From enum import Enum

  • @sean-uw6op
    @sean-uw6op 3 месяца назад +1

    Could you please make a video on python garbage collection

  • @Pawlo370
    @Pawlo370 3 месяца назад

    very good tutorial 👍

  • @Muhammad-zu6lk
    @Muhammad-zu6lk 3 месяца назад

    You'r plane went well!

  • @pratikkshirsagar7152
    @pratikkshirsagar7152 3 месяца назад

    Thanks

  • @makersbee
    @makersbee 2 месяца назад +1

    Share your vscode setup

    • @mukombradon
      @mukombradon Месяц назад

      That's pycharm not vscode

  • @m00n-Child96
    @m00n-Child96 2 месяца назад

    🙏

  • @PennyEvolus
    @PennyEvolus 3 месяца назад

    i didnt even know i needed to know this

  • @drakouzdrowiciel9237
    @drakouzdrowiciel9237 2 месяца назад

    thx

  • @murphygreen8484
    @murphygreen8484 3 месяца назад +4

    I thought the __init__.py was no longer needed? I've never used it. Though I also have issues with pytest when my test files are in a sub directory, so that could be part of it

    • @eduferreyraok
      @eduferreyraok 3 месяца назад +2

      I think the interpreter has added some layer of inference to this… but it can still be buggy sometimes. I.e: i dont get import errors when trying mysql in django, but when including the module at init then the hug goes away

    • @DhavalAhir10
      @DhavalAhir10 3 месяца назад +2

      When you have `__init__.py` file in your folder directory, your folder called as "package".
      When you don't have `__init__.py` file in your folder directory, your folder still count as package but now your folder called as "Namespace Package" instead of "package".

    • @arungumpina4644
      @arungumpina4644 2 месяца назад

      @murphygreen8484 could you please suggest if know any tutorials for pytest?

  • @antoniov845
    @antoniov845 3 месяца назад +18

    Star import is a really poor practice

    • @michaelcummings7246
      @michaelcummings7246 3 месяца назад +3

      If you are basically using everything in a package already it makes sense to use it. If you only need a couple then it might make sense not to so naming conflicts are less likely but personally once my import line gets past 3-4 things I switch.

    • @antoniov845
      @antoniov845 3 месяца назад

      @@michaelcummings7246 there are many issues with star imports. Honestly when I just discovered it I started to use it all over the place since it’s def easier
      But after a few days I changed all that 😀 it makes refactoring etc much harder and imagine solving circular import error with this approach 😀

    • @spaniard13
      @spaniard13 28 дней назад

      Star imports are essential for some Python libraries, where you want to get a whole new "dialect" into your script (versus borrowing specific functionality).
      The best example I know is FastHTML, in which HTML tags are brought into Python to simplify server side rendering with HTMX.

  • @apollowilsons
    @apollowilsons 3 месяца назад

    Can you do blender python? plz

  • @sarundayo
    @sarundayo 3 месяца назад

    Learning python on the go apps?

  • @oneforall4049
    @oneforall4049 3 месяца назад +1

    Which code editor you use

    • @null-0x
      @null-0x 3 месяца назад

      PyCharm

  • @frazer26
    @frazer26 3 месяца назад

    How would you make the connections module accessible with different projects without copying it over to every new project?

  • @akalrove4834
    @akalrove4834 Месяц назад

    I came I saw and i liked

  • @Mark_Point
    @Mark_Point 3 месяца назад

    Do the functions in the new package still need, the if name == main line?

    • @eduferreyraok
      @eduferreyraok 3 месяца назад

      If you want to execute code exclusively at the file, then yes… it applies for all files/imports/packages/modules

  • @mohammadjawad4806
    @mohammadjawad4806 2 месяца назад

    I guess it would be a poor practice or even dangerous to execute a code from __init__ file, right?

  • @arungumpina4644
    @arungumpina4644 2 месяца назад

    Hey @Indently, If I have multiple modules (so many modules) in packages, & our init script has this __all__ dunder method,
    do I need to give each & every module name in __all__ = ['wifi', 'mobile', 'bluetooth', 'module4', 'module5'..... ]
    Is there a better way for us to include all modules in package initalizer? in order for * recognizes all modules?

  • @atmadeeparya2454
    @atmadeeparya2454 2 месяца назад

    what's the code editor

  • @pkuladeep7692
    @pkuladeep7692 15 дней назад

    My favourite Bankai Zanka No Tachi

  • @mlocverm
    @mlocverm 3 месяца назад +1

    👋👋👋

  • @realortigotze
    @realortigotze 3 месяца назад

    Does anybody knows what font he's using?

  • @BohonChina
    @BohonChina 3 месяца назад +2

    you can compare with or wihout __init__.py in two scenarios
    1. HAVING _init_.py, No INSERT of path in sys
    2. NO _init_.py, INSERT path in PYTHONPATH, import module statement after inserting path
    import sys
    sys.path.append('/python/NoInitModule')
    from dir import module statement

  • @majinmarkus968
    @majinmarkus968 3 месяца назад

    its loading some libraries. Innit? :D

  • @seasn5553
    @seasn5553 3 месяца назад +1

    Dude holy I legit was thinking about this an hour ago LOL

  • @miguelvasquez9849
    @miguelvasquez9849 3 месяца назад

    i read the __init__ file is no longer necesary

    • @eduferreyraok
      @eduferreyraok 3 месяца назад +3

      Not its infered, but i would still recommend it since it can be buggy sometimes

    • @DhavalAhir10
      @DhavalAhir10 3 месяца назад

      When you have `__init__.py` file in your folder directory, your folder called as "package".
      When you don't have `__init__.py` file in your folder directory, your folder still count as package but now your folder called as "Namespace Package" instead of "package".

  • @SeigeGoat
    @SeigeGoat 2 месяца назад

    When did python became British?

    • @Indently
      @Indently  2 месяца назад +1

      Since it was founded it was based on a British TV series

  • @LucaCappelletta
    @LucaCappelletta 3 месяца назад +5

    While learning a bit more on how star import works is useful and interesting, star import is really a bad practice. I hope this video doesn't encourage anyone to use it.

    • @ego-lay_atman-bay
      @ego-lay_atman-bay 3 месяца назад +1

      Sometimes it's useful, such as importing c_types. You're most likely going to just import everything instead of using ctypes.c_uint32(). However, they still are not very good most of the time.

  • @rhettsmedia
    @rhettsmedia 3 месяца назад

    For us blind guys you need to speak__or thunder

    • @rhettsmedia
      @rhettsmedia 3 месяца назад

      Also, what is this whole thing is a variable is it a list? What is it is a dictionary

  • @divyakumar8147
    @divyakumar8147 2 месяца назад

    thanks

  • @johneric2720
    @johneric2720 2 месяца назад

    Thanks