Let's talk about '__name__', and '__main__'.
You're going to see this a lot in Python files.
So often in a file, you're going to see something like, 'if
__name__ == "__main__":',
[no audio]
and then 'print("Do a thing")', or maybe it runs some sort of
function. Now, this is actually pretty important when it
comes to running a Python script.
And this is again, fairly common.
You're going to see this quite a bit out there when you start
looking at other people's Python code.
Now, the idea behind this is to determine if the file is
being used as, like an import for a module or a package,
or if it's being used by a regular Python script,
so if it's basically just supposed to run as is.
So is that an import, or is it running like a program?
So I'm going to create a new file, new folder in here, and,
nope, not in there.
And this is going to be called 'Name and Main'. Clear that. 'Name and
Main', create a new file.
And this one's just going to be called 'first.py'.
And I just put it in a new folder just to sort of separate it
from the other files
so we don't get a little confused about which files do what.
Now, how Python works, is you can sort of see this invisible
line that comes up and down in my editor here.
Any code that is the farthest left, Python is going to,
it's going to try to run that,
it's going to at least try to analyze it.
So if we said variable 'name = "Kalob", and the 'course
= "Python for Everybody", then these are going to
be evaluated right away.
But if we said 'def', some function name, 'print("a thing here")',
the reason this automatically doesn't get run is because
this code here is not touching this side.
So Python is going to go through here, and say, "On line 2
there's a variable, on line 3 there's a variable, and on
line 5 there's a function."
But this code here is not touching the wall.
It's actually indented inwards.
And because of that, Python says, "Okay, I understand that
there is a function here, but I'm not actually going to run
this". And in every Python script, there's always this variable
called '__name__'.
So let's go ahead and get rid of some of this stuff.
And let's just print name, 'print(__name__)', and let's run
this in our terminal.
So 'cd' into the folder called 'Name\ and\ Main/', 'ls -la' or 'dir'
if you're on Windows.
And let's do 'python first.py', and it says, this '__name__' is '__main__'.
So now we know, 'if __name__ == "__main__"', that this is a program
calling the file, and we can say 'print()' in here,
"This is a program running the file".
And let's go ahead and run that code in our terminal just once more.
Sure enough, "This is a program running the file".
[no audio]
But just for funsies, let's also add an 'else' statement in here.
'else print("This is an import")'.
And when we run this, we're going to see that nothing actually changes here.
But if we go ahead and create another file, let's call it
'second.py', and it's right beside my 'first.py', I can say
'from first import', that's this file here, I can import something in here.
So let's create a function called 'greeting()', and it's just
going to say, 'print("Hello from first.py")'.
Let's get rid of this 'print' statement, that's not useful anymore.
And so in 'second.py', we are going to import that 'greeting()'
from the first file here,
'first.py', and because '__name__' is not going to be "__main__", because
it's actually just an import,
this should run for us,
and it should say, "This is an import".
So let's go ahead and do this, 'from first import greeting',
and then execute the function,
and let's run the script with, 'python second.py'.
It says, "This is an import", because, hey, look at that,
we imported the file before we did anything, and then we
ran the script.
And because of that, it says "This is an import", and then
ran the function that we're looking for.
[no audio]
And again, if we run 'python first.py',
says, "This is a program running the file", and if we run 'python
second.py', it says, "This is an import".
Now, you actually aren't going to see this 'else' statement too
often. It's pretty rare that you see this kind of 'else' statement.
Usually you just see something like this.
And in fact, you won't even usually see a 'print' statement.
But what you will see is if Python is calling this file as
a program, you will often see a function being called in
here. So this function is already defined, and what this
is saying is just automatically run that function.
So let's go ahead and run 'first.py'. 'python first.py'.
And it says, "Hello from first.py".
And that's because it is now executing this automatically.
And in 'second', we don't actually have anything in here.
So even if you run 'second.py' as a program, it doesn't really care.
We're not telling you to do anything extra.
And if you run it as an import, it's not going to do anything
anyways, because it's just an import.
So if you ever need to determine the difference between when
a file is being run as a program versus a file being imported
as a module or a package, this is the way you would want to do that.
Now you might be thinking, "Why on Earth would I ever need
that?" Well, let's say you have a little mini search engine
or a little web scraper that you've built, and you have a
function in here that scrapes a page, 'scrape_a_page', and it
just goes ahead and gets all the images from a particular
website. So it's going to work some magic in here,
[no audio]
and it's going to 'return "something", '#magic logic in here'.
Now, that's fine.
'scrape_a_page' might automatically want to be run as soon
as you call 'python first.py'.
Now, if you wanted to reuse this, you could copy this whole
file into another project that you're working on,
and you could simply import.
You could say 'from first import scrape_a_page'.
And because this isn't in here, it's not automatically going
to scrape the page. So
now you can actually use it as a regular import, but you can
also use it as a regular program file.
Now, what I would like you to do is test this out, create
'first.py', 'second.py', and in your 'second.py' import from your
'first.py', and have this '__name__ == "__main__"', and then
run a function in here.
Now I'm going to undo a bunch of this because that's not actually
going to do anything for us.
Go ahead, give this a shot just like I did in this video.
You may or may not use this all the time, depending on sort
of which industry you're going to go into with Python.
But it is still good to know because you are going to see this
regardless in Python code.
[no audio]