What is __init__.py file in Python Packages? Explained with Example

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey you this is the awesome from code band and in this video I'm gonna be showing you what is in it dot py in you know a Python package so if you are new to my channel please do subscribe to my channel and click the bell icon here to it so without any further let's jump right into it [Music] you [Music] all right guys so I'm gonna demonstrate the whole process so I have created a main door py file which will be the root Python file where it'll be you know where the things would get executed and what I'm gonna do now is you know create a package a Python package and I'll name it as functions now for those of you who are not who are not familiar with in a Python packages they are just you know a directory that contains some old Python modules and other sub packages so in here in dysfunctions in a Python package I'm gonna create a new module and I will name it as arithmetic p1 so before going into that I'm just creating you know that in it without py file underscore underscore in it underscore underscore dog py and I'll demonstrate the use case by the way let me finish this stuff so in this one I'm gonna Errol you know two methods for just an example def a B add a B then I was just you know return a plus B and in the in this similar way I'm gonna just do you know the subtraction so I'll just say some and return a minus B so that's it guys so I'm also creating a sub package here inside you know this functions Python package so I'll name this as you know simply sub pkg for instance and in a I'll add that in it or py hearing also and what I'm gonna do is i'm just creating you know another module here and i will name it as you know something like new module dot py and what i'm gonna do here is you know I'm just doing and deaf you know friend maybe I could say print hello which does not take any arguments and I'll just you know trend like hello so that's it and just return so that that's all you know about this stuff you know and now I'm what I'm gonna do is you know I'm gonna demonstrate the whole process of any dot P 1 so in automatic Norris or in main dot P Y what I'm gonna do is I'm just importing you know those steps like you know it the functions arithmetic I want to add two numbers for instance so what I will do is you know I'll just write from functions dot automatic import add and I could simply say you know friend add like I'll say just five and six now if I run my code here by the main door py well I get in eleven so you know the use case of this any dot py is that you know you know if it is not present then you know the package will not be considered as a Python package or all right or we could say you know this directory dysfunctions directory has an init or py file and if this was not present then this should not be this will not be considered as a Python package and you know if it was not a heightened package and then you could not do you know these imports so this was the case in titan 3.3 or earlier versions so in those cases this file was mandatory but from there on this is you know this does not make any sense or you could say you can simply delete those any dot py files and you know if you run the core again this one still works so that was the case in Python 3.3 over earlier but now you know this one you know it's not mandatory but say but you know this has got two important functions you know which are still used like you know if I control Z those stuffs and bring it back again now this any dot py suppose I'll do you know I'll take this functions in it or py if I do say friend hello will here print HelloWorld then what I could possibly see is you know in the output has that hello world and what this does is you know in the maned of py if we do import you know functions from functions I could say then it will automatically it will automatically runs this any dot py file so whatever in here is executed so it just printed hello world so this is very important concept so what I could possibly do is you know the first one the first use I'm demonstrating it is just a convenience in a use case it is used for convenience so what I could possibly say here is from dot arithmetic import you know I could say store then it will import those add and sub functions so if I do say like this now if we go to the main door py then you know in from up to hear from functions you know it will execute the functions in it or py so which will execute from door arithmetic imports are means which will import all those stuffs in automatic so now I can simply say you know you know this stuff only from functions in both add I don't want that automatic you know here because you know we already in an importer you know everything from automatic in in its in it dot P Y this is very convenient because you know a user doesn't have to you know or the developer doesn't have to you know explicitly you know go to this functions dot arithmetic in bolt add you know you don't have to explicitly say that he could just say from functions in both add and now if we run the code again we go the same exact stuff now this is one important use case of you know having that any dog dy file so the next one the second important use case is you know nothing but it is nothing but you know if you want to and you know if you want to if you want something to be initialized you know for example logging you know which should be done at the top level meaning you know this is the most top level you know if we are importing the functions this will be the first one to execute you know and here in you could simply you know instantiate or initialize those login configurations for example I could say you know import logging logging dark config so here in I could say you know logging dot config dot dict conflict oh you know you could easily say you know your configurations here you know your login configurations here you know that is you know just you know a good method of initializing your login configuration up at the root level that is you know in the init dot P right now this is the second you know important use case of any door py still and you know if you're just you know doing a blank in it dot py it is not at all you know you needed from you know Python 3.4 I could say or after Python 3.3 you know until just consider you know every every dialogue trees as you know Python packages itself so this is all about you know this any dot py file so hope you understood the video and if you have any doubts please let me know in the comment section and if you haven't subscribed to my channel yet please do subscribe my channel and click the bell icon here to it so thank you for watching we'll see you in the next one [Music]
Info
Channel: Code Band
Views: 10,173
Rating: 4.5760369 out of 5
Keywords: what is __init__.py in python, __init__.py in python packages, __init__.py in python, __init__.py, what is init.py in python, what is the use of __init__.py in python, init.py in python, init.py file in python, __init__ python, __init__.py python, __init__.py explained, __init__.py file, __init__.py file example, __init__.py example, __init__.py used for, __init__.py python example, __init__.py python 3, what is __init__.py file in python, what is __init__.py file
Id: vM3ScLNTGoQ
Channel Id: undefined
Length: 8min 37sec (517 seconds)
Published: Sat Apr 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.