 Welcome back everyone. This is Brian. We're going to talk about import madness in this video. And yes, we are going to deep dive into well insanity here. So what we're talking about is underscore underscore in it. What is it and why do we need it? First things first, we need to make a sub folder and add some code to it. So I'm just going to pop some notes in there. We're going to make a folder and let's literally call it sub. So I'm going to go up here, I'm going to say new folder sub. And inside of our sub folder, gotta make sure you got that highlighted your ID may be different depending on what you're working with. We're going to add two files. The first file we're going to add is going to be called well, very simply test dot key y. And we're going to add some code. And just for the sake of time, I'm going to copy and paste just a simple function do test which just prints out do something here. Really not award winning code by any means. Let's also add another file. And this file has to be named underscore underscore in it underscore underscore dot key y. Remember, whenever you see the double underscores, you're thinking something internal to Python. Now there's a lot of debates out on some forums on whether or not you actually need this and legacy versions of Python you actually do more modern versions of Python. It's debatable, but I'm putting it in here just so we understand it. I'm going to pay some notes here and it's quite a bit of notes. This is needed because we are in a sub folder whenever you see folder. I want you to think in terms of modules. Remember, we've talked about modules just a little bit. But a module isn't just simply a file. It's an entire folder structure. And the problem with creating some sort of folder structure is that Python needs to understand how to work with that folder structure. So in its simplest case, init.py can just be an empty file, but it can also execute initialization code for the package. That's how complex this can get. So if we're working with something like a socket server or a database or something like that, we can actually initialize some code. We could say like port equals 80 or the username, you'd never do this production, but like the username would be admin or something like that. You can actually specify all that right in this file. So we are using this very simply because we want to work with this file and we want Python to understand how to work with this file. So we're going to modify it just a little bit here. We're going to say from dot test import. Star. We've seen import before, but we haven't seen this what is going on here. So what we're doing is saying from dot test. Import star from is just basically saying go out and find this. And we have this little period here if we omit that we may have some bad things happen it may not actually know what we're trying to do here. So we have to put that in there in most cases. And then magically it knows this is the directory if you're ever confused dot is the current directory. And dot dot is the parent. So basically what we're saying is from the current directory and then take a file name test there's no dot p y at the end of it. Go ahead and import and then everything. So we're saying take the entire contents of this file and put it right into Python. Let's go ahead and play around with the imports a little bit so we're going to flip back to our main file here. And we're going to play with imports. So let's go ahead and try and import a couple different ways that we've tried the past. We're just going to simply say import sub dot test as code. And now code dot do test we can actually do this look at us. Now if we run this everything works just fine. But we have skipped over this initialization file. And older version of pythons this may may not work. You might run into some little bit of trouble here. And if you try saying like from the current directory dot sub dot test dot you may depending on your Python version run into something like this invalid syntax. So how do we import this file and if we had other files as well. You guessed it we need this initialization file and there's a special way we have to do this. So what we're going to do is jump right back here. Get rid of all that. And we're going to say from. Remember when you see the from keyword we're saying go find a location so from sub sub is the name of the folder so from this folder. Go ahead and import. Star so we're saying from this folder import everything. Now that's big and scary because what if there's 1000s of files out here that's right we're going to import 1000s of files. And that's where this underscore underscore in it underscore underscore dot py really comes in or the initialization file will just call it for short because it's going to instruct Python on what to do. There may be some files we want to import directly and there may be some files we don't want to import it automatically. We may want to set some variables or some settings or some file structure we would do all of that right here and we could call functions if we had to go is way way deeper in that again we're still in beginner land. And if we really wanted to we could get a little funky with this we could say something like from sub import. And then specific file. Remember that would be test dot py. But you don't need the dot py as code. So we're saying from the folder import a file as this variable looks a little verbose and it seems a little confusing until you really wrap your head around it but it's super super simple once you get it down. The major takeaway here is if you have a sub folder, you should include this underscore underscore in it underscore underscore dot py or the initialization file. If you skip this step, older versions of Python will just simply not work. And you may have some weird issues and if you want to do any sort of initialization and all this file is pretty much a must. Okay, once we've gotten to this point really there's only one thing left to do is actually see it in action. So we're going to call the code here, and say de main. And if you are with me here and you try to do something the IntelliSense in your editor may betray you gotta be a little careful. So I'm going to say main. And for the moment, I'm just going to say pass. Remember pass and Python means do nothing. And if name going go down here equals equals following along from previous video we're saying if the name is equal to main go ahead and run this function automatically that way if we were to import this file, it's not going to kick off this code. Go ahead and get rid of this and we're going to say print. This is the main function. And let's call our imports now because we've done from sub import star meaning import everything. It's going to go in here and it's going to read this initialization file and say from test import star. And it's going to import all of our code we've got this do stuff out here. So do the magic of old copy and paste and grab that jump back out here and say you test and we can just call it directly and it just works. See, do something here. And because we've done from sub import test as code, we can take this and we've just basically said do the same thing but put it inside of a variable now I can say code dot do test. Very simple, very easy works as expected. So which is the correct way which one of these should you use personally I tend to lean towards the second line here simply because importing from everything what if you have a name collision what if there's like a test to file that also has the same do test function you want to be able to separate those out into two different variables or two different scopes remember each file is kind of a island in itself as far as scope goes at least that's how you should think about it in your mind. So the major takeaway from this is well the initialization file matters and if you omit that you may have a bad time in Python land you really need that to initialize how Python does things and there are different ways to import further muddying the waters on import madness. I hope you enjoyed this video you can find the source code out on github.com if you need additional help myself and thousands of other developers are hanging out in the void realms Facebook group. This is a large group with lots of developers and we talk about everything about technology related not just the technology that you just watched and if you want official training I do develop courses out on you to me dot com this is official classroom style training if you go out there and the course you're looking for is just simply not there drop me a note I'm either working on it or I will actually develop it I will put a link down below for all three of those and as always help me help you smash that like and subscribe button the more popular these videos become the more I'll create and publish out on YouTube. Thank you for watching.