Importing Your Own Python Modules Properly

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome back in today's video we're going to learn how to properly import our own custom python modules into our scripts and also with the role of the init py files is in that context so let us get right into it [Music] all right so today we're going to work with multiple files and multiple directories and please don't be confused by my development environment I'm using neovim inside of the windows subsystem for Linux so inside of a terminal on Windows but that is completely irrelevant to you you can use whatever you want you can use vs code pycharm notepad plus plus that python idle doesn't matter all you need is a directory where you can create multiple python files and directories and you need a python editor and a python development environment that's basically it you just need to be able to run Python scripts I'm going to use this environment but don't be confused by that so the first thing you want to do is we want to create a file called main.py and this is going to be obviously our main file here we're going to run code and the goal is now to have functionality in different python files that we can then import into the main file so for example let's create a second file here let's say we have um a file I'm going to call it functionality.py we just create this file here um and essentially we're going to open this now in Split mode here I'm gonna just Define a couple of arithmetic functions so I'm going to say def add now let me just briefly before it fires up again disable the auto completion so we're gonna have ADD taking parameters X and Y returning X Plus y very simple functions and we're gonna copy this now and we're gonna have the same function here for sub we're gonna have the same function for malt and we're going to have the same function for diff and we're going to change the respective operators here so those are the functions that we would like to use in the main file how can we do that now when we have just one file this is kind of simple all we need to do is we need to say uh from functionality import and then I can say for example add then I can print print at 10 and 20. so that's quite simple um I can now open up a terminal here I navigate to the directory that I'm working in and I can now run the python file again don't be confused by the environment all I did is I created two python files in the same directory in a random in the terminal so you can do that as well um and you can see it worked so now I use the add function of the functionality python uh file and I used it in the main file without having the function defined in the main file by just saying from functionality import at so this is kind of simple nothing too complicated here um now what I can also do is I can say from functionality import Star and then I can also use add and I can also use sup and let's see if that works Python 3 main py you can see it works without problems um so that is how you do that if you just have a file in the same directory now let's say you don't have just a single file but you have an actual module so you actually have a directory with multiple python files that have different functionality uh in those files so let's say here I'm going to create now a new directory let's call it um other module and inside of that I'm going to create a python file called second.py I don't know just just some file here and let's open this file and let's say we have a function my function here and let's say I just want to print hello with this function so in order to use this function now I don't have to do anything fancy I just go again to my main file I say from uh and in this case other module import and um I can just import here now the second so the difference here is that I'm importing now the full file so if I have multiple functions this is not a function here I'm saying from the file functionality import the different functions whereas here I'm saying from the module from the directory other module import one of the Python files and if I now want to access the function I have to say second dot my function like this in order to execute it in order to call it or of course alternatively I can also say import uh or actually I can say from uh other module dot second import everything and then I could just run I could just call the function without referring to Second specifically so that works as well the interesting thing now is what if we have multiple files and this is maybe something new to you because that's up until now this was actually quite clear let's say I have another file here third Dot py and um I have I don't know another function and this time it prints ABC or something like that and uh now let's say I want to do something else I want to go into my main file and I want to say from other module import absolutely everything so I want to import second and third and I want to be able to do something like second dot my function how do we do that because if I try to do it like this if I just say from other module import everything and I want to import those two files you can see it doesn't work and this now is why we need the so-called init py file so underscore underscore init underscore underscore py files that you see in so many projects that you've seen so many packages sometimes it's Auto automatically created in pycharm for example this file has the functionality to actually make a directory into a module so if I go here into other module and I create this file so underscore underscore init underscore underscore Dot py if I just create it this now makes this directory to a module so now this is recognized as a module and inside of this file I can also write the following line I can say underscore underscore all underscore underscore equals and then I can provide a list with strings and these strings refer to the file name so I can say here second and third and now this already this file here this init py file with this line of code being in the same directory as those two files this will allow me to use this notation from other module import Star because what happens here is the Stars decoded into second and third and if I now run this you can see this works so this is how that works another thing of course is you can go ahead and create a sub module so I can create another directory sub module for example inside of other module and here I can create a file let's call it fourth dot py um and if I open it and I say last function print I promise there you go now I can go into my main file and say from other module.submodule import for example fourth so this should work I think if I now say um fourth dot last function like this this works as you can see and I think it should also be possible to say uh dot fourth import and then last fct then we don't have to use default so as you can see this works as well now one last thing that I want to show you is a notation I'm not sure if we actually need it anymore but one thing at least to make it Backward Compatible compatible and if you publish your own package this is also always a good thing to do you can also write the import statements of the respective module here inside of the npy file for clarity so you can say from dot another uh not another sorry this is my prepared code uh from the dot second we want to import uh what did we have here my function so my function and from third import another function what's the name I think so this is how this is done professionally um another function another function there you go so this also works and this is how you allow for the usage of the uh of the Wild Card import here so this is how you do that and those are the basics of working with your own custom modules in Python you can just import them if they're in the same directory by just specifying the file or if you have a module structure it's always useful to Define init py files to allow for this special type of import but yeah that is how you do that in Python so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you in the next video and bye thank you [Music]
Info
Channel: NeuralNine
Views: 211,265
Rating: undefined out of 5
Keywords: python import, python import module, python import own module, python import own file, python import file, python init.py, python __init__.py, what is __init__.py, python, __init__.py
Id: GxCXiSkm6no
Channel Id: undefined
Length: 9min 56sec (596 seconds)
Published: Wed Jul 06 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.