What does '__init__.py' do in Python?

  Рет қаралды 93,350

Indently

Indently

Күн бұрын

Пікірлер: 94
@umka7536
@umka7536 2 ай бұрын
Thanks for covering the role of __init__.py in the import of a module. Because a lot of video tutorials skip this information.
@useronetwothree-z6f
@useronetwothree-z6f Ай бұрын
fr
@arkie87
@arkie87 2 ай бұрын
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....
@danialothman
@danialothman 2 ай бұрын
that one class I ignored, it was this one in 2021. i got it now 😂 thanks for this!
@BohonChina
@BohonChina 2 ай бұрын
you should mention the difference between having _init_.py and not having one in your module directory
@luisdominguesforprogramaca3221
@luisdominguesforprogramaca3221 2 ай бұрын
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 2 ай бұрын
@@luisdominguesforprogramaca3221 NO _init_.py, INSERT path in PYTHONPATH, import module statement after inserting path will behave like a package too.
@DhavalAhir10
@DhavalAhir10 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".
@creed404yt9
@creed404yt9 2 ай бұрын
Rewatch the video
@Omsip123
@Omsip123 2 ай бұрын
How about explaining the difference in your comment?
@laitei-e40
@laitei-e40 2 ай бұрын
Great! Loved it, my hero 🎉
@petsandpaws8906
@petsandpaws8906 2 ай бұрын
Crystal clear. Thank you for explaining it very simpel!
@dragweb7725
@dragweb7725 2 ай бұрын
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_ 2 ай бұрын
Same. Had to use sys.path to import packages from another folder yesterday. My brain exploded
@ego-lay_atman-bay
@ego-lay_atman-bay 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 2 ай бұрын
@@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 2 ай бұрын
@@dragweb7725 and what is the use case for such a project with that kind of folder structure?
@lisaw7074
@lisaw7074 26 күн бұрын
This is so helpful and clearly explained! Thank you very much!
@onlysin530
@onlysin530 Ай бұрын
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
@prach-poom
@prach-poom Ай бұрын
You're awesome. This one has a lot of great tips
@thefanboy3285
@thefanboy3285 2 ай бұрын
Thanks. Mind refresher, Life saver.
@mukisajohnmary
@mukisajohnmary 2 ай бұрын
Wonderful explanation. Thanks 🙏
@purplemonkeydishwasher5269
@purplemonkeydishwasher5269 Ай бұрын
This is amazing. Im completely rebuilding an app Im working on based on this!!!
@awetinoawets9290
@awetinoawets9290 2 ай бұрын
Very useful 🙏🙏🙏
@michaelangellotti5741
@michaelangellotti5741 2 ай бұрын
Quick and accurate. Thanks.
@average_paniker
@average_paniker 2 ай бұрын
Clearly explained. Thank you!
@nobody2937
@nobody2937 2 ай бұрын
Thank you indeed... Extremely clear, concise... Thank you ...
@mikecanaday4888
@mikecanaday4888 2 ай бұрын
Good job! Pycharm was a nice touch since that’s the environment that many use.
@EquiliMario
@EquiliMario Ай бұрын
The __all__ dunder method is something i didnt know yet. Neat
@ml_with_pranay
@ml_with_pranay 2 ай бұрын
I enjoy your videos a lot! Keep posting😊
@tejasmarvel2023
@tejasmarvel2023 Ай бұрын
very clear video. thanks
@HiltonFernandes
@HiltonFernandes 2 ай бұрын
Great video. Congrats !
@7th_dwarf542
@7th_dwarf542 2 ай бұрын
brilliant! 👏
@AdrianCortes-b7f
@AdrianCortes-b7f 2 ай бұрын
Veryw ell explained, Thank you!
@AlWardani-k2h
@AlWardani-k2h 2 ай бұрын
❤❤❤❤❤ awesome
@davidlu1003
@davidlu1003 2 ай бұрын
I love you, great videos, and please keep going.😁😁😁
@FighterAceee94
@FighterAceee94 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).
@_Garm_
@_Garm_ 2 ай бұрын
aveasome thank you, would like to learn more about it :D
@goobiie
@goobiie 2 ай бұрын
Very useful video
@basilchinedu144
@basilchinedu144 2 ай бұрын
Thanks a lot, very helpful.
@odevperovano
@odevperovano 2 ай бұрын
Excellent, thankyou!
@elastvd7503
@elastvd7503 Ай бұрын
Super , thanks
@banjijohn
@banjijohn 2 ай бұрын
❤ thank you 🎉
@benmatt8940
@benmatt8940 2 ай бұрын
Utterly usefull
@Pawlo370
@Pawlo370 2 ай бұрын
very good tutorial 👍
@giocic94
@giocic94 2 ай бұрын
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?
@timbrap4693
@timbrap4693 Ай бұрын
I do that too!
@Muhammad-zu6lk
@Muhammad-zu6lk 2 ай бұрын
You'r plane went well!
@HoSza1
@HoSza1 2 ай бұрын
so a package is a container that contains an __init__ in it! 😂
@BohonChina
@BohonChina 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
@sean-uw6op
@sean-uw6op 2 ай бұрын
Could you please make a video on python garbage collection
@PennyEvolus
@PennyEvolus 2 ай бұрын
i didnt even know i needed to know this
@akalrove4834
@akalrove4834 28 күн бұрын
I came I saw and i liked
@michaelrstudley
@michaelrstudley 2 ай бұрын
Fantastic video. How do you feel about placing all imports needed for the files inside? For example... From enum import Enum
@m00n-Child96
@m00n-Child96 2 ай бұрын
🙏
@pratikkshirsagar7152
@pratikkshirsagar7152 2 ай бұрын
Thanks
@makersbee
@makersbee Ай бұрын
Share your vscode setup
@mukombradon
@mukombradon 8 күн бұрын
That's pycharm not vscode
@murphygreen8484
@murphygreen8484 2 ай бұрын
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 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 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?
@drakouzdrowiciel9237
@drakouzdrowiciel9237 Ай бұрын
thx
@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?
@oneforall4049
@oneforall4049 2 ай бұрын
Which code editor you use
@null-0x
@null-0x 2 ай бұрын
PyCharm
@atmadeeparya2454
@atmadeeparya2454 2 ай бұрын
what's the code editor
@apollowilsons
@apollowilsons 2 ай бұрын
Can you do blender python? plz
@frazer26
@frazer26 2 ай бұрын
How would you make the connections module accessible with different projects without copying it over to every new project?
@Mark_Point
@Mark_Point 2 ай бұрын
Do the functions in the new package still need, the if name == main line?
@eduferreyraok
@eduferreyraok 2 ай бұрын
If you want to execute code exclusively at the file, then yes… it applies for all files/imports/packages/modules
@mohammadjawad4806
@mohammadjawad4806 Ай бұрын
I guess it would be a poor practice or even dangerous to execute a code from __init__ file, right?
@antoniov845
@antoniov845 2 ай бұрын
Star import is a really poor practice
@michaelcummings7246
@michaelcummings7246 2 ай бұрын
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 2 ай бұрын
@@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 5 күн бұрын
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.
@sarundayo
@sarundayo 2 ай бұрын
Learning python on the go apps?
@mlocverm
@mlocverm 2 ай бұрын
👋👋👋
@realortigotze
@realortigotze 2 ай бұрын
Does anybody knows what font he's using?
@seasn5553
@seasn5553 2 ай бұрын
Dude holy I legit was thinking about this an hour ago LOL
@majinmarkus968
@majinmarkus968 2 ай бұрын
its loading some libraries. Innit? :D
@LucaCappelletta
@LucaCappelletta 2 ай бұрын
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 2 ай бұрын
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.
@SeigeGoat
@SeigeGoat Ай бұрын
When did python became British?
@Indently
@Indently Ай бұрын
Since it was founded it was based on a British TV series
@miguelvasquez9849
@miguelvasquez9849 2 ай бұрын
i read the __init__ file is no longer necesary
@eduferreyraok
@eduferreyraok 2 ай бұрын
Not its infered, but i would still recommend it since it can be buggy sometimes
@DhavalAhir10
@DhavalAhir10 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".
@rhettsmedia
@rhettsmedia 2 ай бұрын
For us blind guys you need to speak__or thunder
@rhettsmedia
@rhettsmedia 2 ай бұрын
Also, what is this whole thing is a variable is it a list? What is it is a dictionary
@divyakumar8147
@divyakumar8147 Ай бұрын
thanks
@johneric2720
@johneric2720 Ай бұрын
Thanks
"None" and "NoneType" Explained In Python
6:37
Indently
Рет қаралды 16 М.
5 Tips To Write Better Python Functions
15:59
Indently
Рет қаралды 114 М.
Enceinte et en Bazard: Les Chroniques du Nettoyage ! 🚽✨
00:21
Two More French
Рет қаралды 42 МЛН
Tuna 🍣 ​⁠@patrickzeinali ​⁠@ChefRush
00:48
albert_cancook
Рет қаралды 148 МЛН
It works #beatbox #tiktok
00:34
BeatboxJCOP
Рет қаралды 41 МЛН
Python's 5 Worst Features
19:44
Indently
Рет қаралды 113 М.
Intermediate Python Tutorial: How to Use the __init__.py File
6:22
Eric O Meehan
Рет қаралды 146 М.
5 Signs of an Inexperienced Self-Taught Developer (and how to fix)
8:40
Python Programming ROADMAP :How to Become a PYTHON Developer (2024)
5:41
Tisfoulla Academy
Рет қаралды 13 М.
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 337 М.
Пакеты в Python. Файл __init__, переменная __all__
11:48
5 Good Python Habits
17:35
Indently
Рет қаралды 666 М.
Modern Python logging
21:32
mCoding
Рет қаралды 210 М.
10 Nooby Mistakes Devs Often Make In Python
24:31
Indently
Рет қаралды 69 М.
Enceinte et en Bazard: Les Chroniques du Nettoyage ! 🚽✨
00:21
Two More French
Рет қаралды 42 МЛН