 Welcome back, everyone. This is Brian. We're going to talk about the main function in Python. Now in case you're wondering what a main function is in most programming languages, you'll have something like this. And all this is is a function literally called main. And when your application execute, it's going to be the first function or the entry point to run. And without a main function, your application simply won't work. Well, as we've seen with Python, you don't need a main function. This works just fine. So how do you get your code to run automatically? That's really what we're going to be talking about in this video. Let's go ahead and take a look at this. So first thing we need to do is determine how our script is running. If you've been following this series in the playlist on YouTube, you know that you can run your script directly and your script can be imported. So we want to make sure we know exactly how the script is being run. So let's go ahead and go print. And we're gonna say F sort of format this out, say name equals. Now we're going to use a special variable underscore underscore name. Whenever you see that double underscore, this is built into Python. We're going to take this and print this out. Let's go ahead and change this to file. And I stated in a previous video, this may or may not work. So if you have problems with underscore underscore file, you're going to want to watch that previous video where I talked about using the system arguments that are handed to your script. This should just work just fine. Most of the time. Let's go ahead and run. You can see how our name is underscore underscore main. So remember, double underscore means it's built into Python. And the file is well the file name that we're currently running. This spoiler alert could change. So don't depend on this. And what I mean by that is if we are calling our code based off an import in a special way, the file name will not actually be the name of the script we're currently in. So you got to be a little bit careful there. But I want to highlight those things. The big takeaway from this is our name is underscore underscore main, which means we know Python is running this file directly. This is the main script file. Now let's go ahead and create some code here. Our script really doesn't do anything. So we want to do something. We're just going to make a test function. I don't really care what it does just as long as it prints something out on the screen so I can see it's running. Now we're going to make a main function here. Notice the name is main. Go ahead and print this out. And let's just for giggles, go ahead and call our other function here. So really what we want to do is we want this script to run automatically run this main so we know what we're doing and then call this other function and start some chain of events do some code processing and things like that. So let's go ahead and let's clear all this out. And let's see what a lot of people who are new to Python make a mistake they go oh I know what I'm doing and they go ahead and run and nothing happens. Oh that is a bummer. So yes in Python the main function really isn't called automatically we have to call it specifically. Now you may be thinking I know what I'll do I'll just go down here and then call main directly. And it will work watch we'll run it and it runs this is the main function this is the test function so we are calling it. The problem comes in with this name right here. We're going to demonstrate right after this section. We're going to show you what happens if we run this from a different script we don't necessarily want this code execute, meaning if we take this file and import it as a variable in another file I don't want this function firing off. So we want this to run automatically but only if a certain condition is set. Go ahead and say main. And we're going to print this code out automatically this is my ID you may have to type this we're going to say if underscore underscore name equals main then and pass it's just going to do nothing. So I'm going to take this call our main function directly. Go ahead, save and run. And sure enough, it runs our main function. So it's saying the name is equal to main. So we know Python's running this directly a main, go ahead and call this. Once you've wrapped your head around the difference between underscore underscore name equals underscore underscore main. And you know that means that Python is running this directly. We can do something a little bit different. We can actually take this code file. We grab some notes here. And we can run this from a different script just to see the difference here. So we're going to create a file called test.py. And I'm going to make some mistakes just to show you what's going on. I often make mistakes but legitimately this time I am trying to show you what's going on. So the first thing we're going to do is we're going to import this file here. So let's go back here. And I want to say import Python. Notice how IntelliSense is helping us out as code. Now from our previous video you know that what we're doing is we're taking this file in its entirety and converting it into a variable which we can use. Before we go any further let's run this. Notice how we get an invalid syntax. It's got this little it's called a carrot right here is pointing up at this bracket. So what it's telling us is it doesn't like this. It's simply our files naming convention needs to change. So what we can do is let's right click this and call this code file.py. The functionality has not changed. So for example we'll run code file which used to be the Python 3-28. And you see it's working as expected just the file name has changed. So the functionality hasn't changed just the name. We're going to have to go back in here and change the name. Clear this out. And let's run this test.py. You can see how right off the bat what's happening here is name, code file, file name, code file. What is going on here. We ran testpy. But it's printing this out. So when we hit import, it's going to go through and start reading our file from the top down and it's going to actually take some actions. So this is a little bit dangerous here. And just for illustrative purposes, I'm going to grab this go in here. And let's say let's call this test name and test file just to separate those out. Clear this and rerun. Now you see they're both firing off even though I'm running test.py. Import is happening first. So it's saying import code file it's going over to code file executing this code it's not executing our functions. And because the name is different it's not executing this see name is code file. So the name is no longer underscore underscore main the name is well simply the short version of the file name code file doesn't have the directory doesn't have the extension of dot py. That's why we need this if name equals underscore underscore main. So now in here, we can actually call this directly. Save run and see it in action. Sure enough. This is a test function main never fired off. This may seem a little bit confusing. So quick recap, you have to be a little careful how you name your files if you're going to use them as an import. Some characters are just simply illegal like the dash you saw earlier. If you're going to run automatically you want to check the name is equal to underscore underscore main because if we're running this file as an import, it's going to give us the name of the file with no underscores in it. This is not guaranteed to be the name of the file Python considers anything we want. The point being underscore underscore main means that Python is running this directly and just illustrate that we're going to run the code file again and see name is underscore underscore main. 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 udemy.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.