What should you write into the __init__.py file? 2MinutesPy

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

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

  • @AntonioRonde
    @AntonioRonde 2 месяца назад +107

    Putting logic in the __init__.py file sounds like an excellent opportunity for a "wtf is my code doing"-moment in a later debug session. Constants could be nice.

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

      Could be and sometimes it can be helpful

    • @LooksGoodMusic
      @LooksGoodMusic 2 месяца назад +8

      Surely implicitly, silently performing side effects when a module is imported is not a good idea

  • @blakephillips8649
    @blakephillips8649 2 месяца назад +102

    Notice that he imported functions and not whole modules. Be careful when including whole modules since that can cause "circular import" errors. It's probably best practice not to import whole modules, but wanted to throw that nugget of personal experience out.

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

      But isn’t it the case that to be able to import a function from a module, the whole module must be read?

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

      no, this doesn't fix the problem, you have to load the module first to then import the function. The thing is, if you import module, but not the functions, you may not actually get the import cycle error if it's a calltime dependency. The import cycle actually becomes a problem when you try to import a function from a partially initialized module, where the function isn't defined at the time you tried to import it, that's where you get an error.
      You shouldn't re-export functions in __init__.py files, because every single module in a package first imports that __init__.py file before being imported. Re-exporting functions will just cause every single module in your package require every other module in the package, which is the cause of subtle import loops. Your modules should depend on the __init__.py file, not the other way around.

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

      @@FastKnight401 Ohhh Thanks, that makes mores sense, i was hahing that exact problem

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

      ​@@FastKnight401 i do not understand, could you explain with more details or share any thread on stackoverflow od Smith more?

  • @FastKnight401
    @FastKnight401 2 месяца назад +15

    Something to be wary of is that every single module in a package has an implicit dependency on the __init__.py file, so re-exporting modules actually creates a fake dependency where every module in the package requires every other module in the package, leaving you with very subtle import loops. Because of this, I don't recommend re-exporting modules in __init__.py files at all. The modules should depend on the __init__.py files, not the other way around.

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

      Importing modules within init.py file means there is no need to explicitly import them into another code base after you imported the whole package. These modules are instantly ready to use as soon as the package is imported into another code or file

    • @FastKnight401
      @FastKnight401 2 месяца назад +6

      @@2MinutesPy the problem is the fact that those modules inside a package actually require the __init__.py of the package by default, even if you don't explicitly import it. By re-exporting those modules, what you're doing is that every single module inside that package requires every other module in the package, which can lead to very subtle import loops. I can only imagine how frustrating it is to deal with these types of import loops. This happens because the __init__.py module imports all the modules, and the modules require the __init__,py file to run first.
      This is why the module should be depending on the __init__.py file, not the other way around. If you still want the shorter names, you can make another fake package and import the modules in the fake package's __init__.py file. You'll probably have the real package named "_" while the fake package named "".
      I don't recommend re-exporting functions and stuff in the real package's __init__.py files to avoid the import loop problem. You may be able to debug import loop issues, but it could be restricting, and it's really subtle if you don't understand that modules implicitly import the __init__.py file of their package.

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

      please, let me make sure od i understand correct,
      If I import pandas in the __init__.py file, is the pandas library available in all modules on the package?

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

      @@dynamicgrad3820 not really. So, say we have a structure:
      package_a
      __init__
      module_a
      module_b
      and you do
      from package.module_a import
      what will happens is that the __init__ file will run first, then the module_a file will be imported. The stuff inside the __init__ file will not be accessible to the module_a file unless module_a explicitly imports package_a. The main problem is the implicit import of the __init__ file from the module_a file.
      If the __init__ file re-exports the functions in module_a and module_b, then importing module_a will always import module_b, even if module_b doesn't depend on it. This is because the __init__ file is run before import module_a, and the __init__ file imports module_a and module_b. The same way, importing module_b will always import module_a.
      This design decision is the reason why re-exporting modules may cause import loops. I could give an example of this if you want one, it's really tricky to debug.

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

      @@FastKnight401 thank you! It would be really helpful, if you could provide aby example

  • @TheTmLev
    @TheTmLev 2 месяца назад +15

    Never ever ever put side effects and global variables to your __init__.py file

  • @spidey2468
    @spidey2468 2 месяца назад +10

    Now, another thing I can flex to my teammates of Junior Devs ;) Thanks

  • @oussaber
    @oussaber 2 месяца назад +5

    Hey, fantastic animations! Which tool did you use to create them?

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

    I am new in python, a year more or less, I used to program in perl. Anyway, I like the idea of logging modeule in init. Any advice about that? Sorry my english, si not my native lang. Thanks for the video.

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

      No problem. You can monitor your package using logging to log key events.

  • @fortnort7350
    @fortnort7350 2 месяца назад +4

    Isn't there also a thing where you check if the __name__ == "__main__" to see if something is being run as a program or a module?

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

    Credentials and tokens too, but only if you are a true member of the python gang

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

      Yeah using env variables

  • @vaitesh
    @vaitesh 2 месяца назад +4

    Many thanks!

  • @sachinpal5823
    @sachinpal5823 2 месяца назад +6

    Nice one

  • @MathsSciencesTech
    @MathsSciencesTech Месяц назад +2

    2MinutesPy making a Pi-minutes long video 🤔

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

    Nice good tips thx u so much❤

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

    The fact that this video has a length of 3.14

  • @GrzegorzZygan
    @GrzegorzZygan 2 месяца назад +4

    For impatient. Nothing.

  • @KieraTelecky-e7l
    @KieraTelecky-e7l Месяц назад

    Tiffany Extensions

  • @trevoro.9731
    @trevoro.9731 2 месяца назад

    Delete a random file from system32 if you are using it on Windows.

  • @RobertAthena-g5r
    @RobertAthena-g5r Месяц назад

    Eldred Skyway

  • @FactFinders-of3mu
    @FactFinders-of3mu 2 месяца назад +1

    U r smart

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

      Thanks for appreciation

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

    Why 3 minutes?

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

      Because the list is endless 😅😅

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

    👍 (normaly Id say a lot of stuff but my battery is all time low)

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

    Actually from Python 3.3 you don't need to use __init__.py file

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

      Yeah right. Init.py file can help you get rid of circular imports error within a package and there are many scenarios where it could be helpful

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

    this video isn't two minutes, very disappointed

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

      Apologies

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

      @@2MinutesPy false advertising these days smh

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

      Then make yours. There is nothing wrong with the video’s content. Fly a kite…

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

      @@BhekizweShongwe mfw you don’t understand a joke