 Welcome back everyone. This is Brian and we're talking about imports in this video. Imports allows our code to be used by by other scripts. And it allows us to use other people's scripts. But most importantly, really, what importing allows us to do is simplify things, we can actually now structure our code, and then import things as we need them. Now the import system is actually pretty complex. So in this video, we are just scratching the surface, we're going to talk about some real high level issues. And I want to show you how to split your code into different files. We're going to have to split this into multiple videos throughout this series. So we're going to continually go back to the import system and add to the complexity over time. Let's dive in. First things first, we want to make a separate file. So I'm going to just pop in some notes, we're going to create a file. And I'm going to leave this comment in there for anybody who just doesn't watch this video and just finds the code out on GitHub, it's just going to say go ahead and look at my code.py. And we're going to go over here and we're going to actually make a new file. We're going to give it the name my code.py. And inside of this file, well, we're just going to add some code here. So I'm going to say name equals whatever your name is why I horribly screwed that up. Can't even spell my own name, right? Actually, I spelled it right, I just had a case sensitivity wrong. We're going to make a function called greet. And I'm just really freestyling this. I don't really have anything in mind. We're just going to add a bunch of code and functions here. So I'm just going to make a function called greet, what's going to print out hello, and then whatever the name is, notice the scope here name is actually up in the global scope of this file. I'm going to say death. And let's go ahead and say to file, we've already learned how to work with file system in a previous video. If you missed it, go ahead and go back in the playlist and watch that video. Let's say file name, global name really don't need to say global because we're not trying to modify it, we're just trying to read it. Python is pretty leaning about that. But just to be safe, we're going to say global. And then open file name, we want to write to that file again, if you completely skip that video, I would highly encourage you to go watch it. And we're just going to say F, right, and we're just going to write everything out to the file. In this case, we're just simply writing the name, we take this very simple pattern, and just change it around. So now we have from file, and we are going to open that in read mode. And then we're simply going to do the exact opposite here. Quick refresher, when we say with, it's going to remove this when we're done with it. So it's going to automatically close that file for us. So we don't have to worry about any of that complexity. Okay, so our file is all done. And it's not super hard. It's just got a variable and some functions. Let's go back to our original Python file here, does absolutely nothing. Actually, let's jump back here and make sure you save that. Now, what we're going to do is use import as and I think we've touched base on this, but we haven't really dove into what's going on here. So I'm going to say import. And you want my code as and let's just give it a name person to be anything we want, we could call ice cream cone kitten code, whatever we want. But what's really going on here is import is saying import this module. Now modules of concept, we haven't really covered and we are going to cover in a future video. But right now when I say the term module, just think about external code code that's outside of our file. So import is going to look in and invisibly, there is a little right there. It's going to look in the current directory for something named my code and directory structures with imports gets a little confusing, we're going to cover that in a future video. So everything right now is in the same folder at the root of your project. So import this file, there's an invisible dot py at the end of this, which is of course, my code. Now if you misspelled this, it's simply not going to find it. Now we're saying as person. So what it's doing here is it's importing this entire file. This thing that we've already written has one variable. So it's now creating a variable called person. You can see how person is now my code. So that entire file is now sitting in this variable for us to use. It gets way more complex than that. But that's about as simple as I can explain it. Basically, we're taking this entire file and converting it into a variable. Now at this point, I would highly encourage you to test this just simply run it. It should do absolutely nothing. But if you had some sort of misspelling or something, you're going to get something like this module not found error. And then no module named, named is your key here. The name is misspelled. So let's go ahead and fix that. Just wanted to demonstrate that. Let's talk about some scope issues. We can run this and see that it's working as expected. Let's go ahead and clear that out. Let's go ahead and use our global. So I'm going to say global name. Not in error. Thank you, IntelliSense global name. Go ahead and print this out. What's happening here? Name error name name is not defined. Wait a minute what in our file we have name and we're using it via the global scope. So what's really going on here. And what we're trying to illustrate is that a module has its own scope. So the global scope of the module is not the global scope of your script or your application. They are two completely different scopes. So even though we can explicitly try to shove this even though it's already a global we can try to shove it in the global. It really doesn't help us. It's still not defined. Now if I do something like this, comment that out run this what happens. Same thing name name is not defined. So it doesn't really matter which way you try. Oh, that is super, super frustrating if you're newbie. So how do we get the name out of here or remember, we have taken this entire file and converted it into a variable we have made pretty much our own data type. So we're gonna go ahead and print person or whatever you name that. And now we want the name attribute. And it automatically knows it's a string. So life is now going to be good. Let's plop a note in here just so anybody out and get hub land can see easily what's going on, clear this out and run. Oh, it's because I still got this in there. Sorry about that. Let's try that again. Alright, so there we go. Brian Karen. So if you were like me and you forgot to comment that out, you're gonna get that nasty error. Very good illustration of scope in terms of imports and modules. Anytime you have a new file, think you have a different scope. There are ways to jump between them. But for realistic purposes, they are two different scopes and you should treat them as such. Last thing we need to do is really just do some testing. So we're going to go ahead and test this code. I'm going to add some spaces there. Just get some screen real estate. Let's go ahead and say person name equals and I'm going to switch this to Brian instead of Brian Karen's. And then let's go ahead and do person dot greet. All that function. And let's just do person dot to file. Let's give this a very simple name test dot xt. It's just going to dump that name out to a file. Let's go ahead and say person dot name. What's Tammy, go ahead and do the greet again, just so we can see that we did change that variable. Let's go ahead and load that file. Now as long as our file name matches up, it's going to load it just fine. Go ahead and greet again, just so we can see that we did actually load that name from the file. Let's go ahead and run this see what happens. So the original name is Brian Karen's I switched it to Brian, switched it to Tammy and then we loaded it from the file and we can see we now have this test dot txt out here. So in a nutshell, just to recap, imports is a really, really great way of structuring your code and it really simplifies things. It does have its complexities, which we're going to cover in future videos, and we've touched upon a little bit here with scoping issues. It gets way more complex than that because what we're really touching on with imports is the cornerstone or the foundation of modules. And unfortunately, before we can dive deep into modules, we got to learn other things first like well classes, which is coming up very, very soon. 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 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.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.