Skip to content
PodSubs
ShowsPeopleGuestsJobsNewsEvents
More
BlogPodCred
Menu
ShowsPeopleGuestsJobsNewsEventsBlogPodCredNetworksSign in
Join nowSign in
  • Shows
  • People
  • Jobs
  • News
  • More
    • Guests
    • Events
    • Blog
    • PodCred
    • Networks
    Join nowSign in

Daily Python Projects Podcast

Daily Python Projects Podcast artwork

What is "if __name__ == "__main__" for?

Jul 26, 20247 minAudio

This is a public episode. If you'd like to discuss this with other subscribers or get access to bonus episodes, visit dailypythonprojects.substack.com/subscribe

Audio and video play straight from the show's own hosting. PodSubs stores and serves no media.

Transcript

From the show. Tap a time to play from there.

SPEAKER_00if name is equal to main perhaps you have seen this line being used in many applications in python and it's a very tricky line which for beginners it's very hard to understand what this does so in this video i want to fully explain what this line does what is its purpose in python So let me introduce you to this application because you want to understand this from a real world perspective with a real example. So I'll run this application and this is a Flask web app.

SPEAKER_00So it allows the users to convert miles to kilometers. So pretty simple. Now, what happens if we remove this line? So I'm just commenting that out and I have to unindent this. So this line now, which is responsible for initializing the application, it's not dependent on this. What will happen now? If I refresh this app, things will work the same.

SPEAKER_00So it seems like this line is unnecessary because the application works either way. Yes, true. In this simple example, it is unnecessary. But imagine this. We have this function here which converts miles to kilometers. So it returns kilometers, a simple Python function. And this is being used here. Now, what if we expand this Flask application and perhaps we would have another component of this web app let me call this

SPEAKER_00app2.py and this other component perhaps it imports let's say from app import miles to kilometers. So we are importing, let me open this to the right, we are importing from app dot py. So from app, import miles to kilometers, this function, and then perhaps this app who uses that function in some way, but let's keep it simple. Let's say it simply calls that function and print the outputs in the command line.

SPEAKER_00Right? Now, what happens if we don't have this line? see what happens i'll stop the application from running so the app is not running now right if i refresh this nothing is running what if this component is being executed this script so let's run this file app2.py see what happens now

SPEAKER_00So the Flask application is running, but we don't want that. See if I reload the page. The Flask application is running. Why is the Flask application running? Well, that is because when you import a module or a file, you can say, so we are importing app here. When you import app, this entire script will be executed and if this is executed this runs this line

SPEAKER_00initializes the application and that's why we see the app here But that's perhaps not your expected behavior of the application as a programmer. So you need a way to control the behavior of this script. And the solution is this. So if we do this now, if we put this under this, and we go here and stop this and we run this file again run python in terminal now

SPEAKER_00the flask application is not running anymore all we get here is the output of this print function so 16 point something So we successfully used this function without actually initializing the Flask application. But when we run this file directly, so when we run this particular file app.py, then the Flask app is running.

SPEAKER_00So now it's running. that's the expected behavior. So this line does the magic and how does it do the magic? Well, I'll explain you how. So the magic here is that, let me explain it to you by printing out name. So I'm printing out this variable, right? Let me maybe put some visible output here so we can locate it easier here hey hey for example and see we got this

SPEAKER_00hey hey main so main this is a string so the value of name is main but this is only the case when we run this script directly let me stop this and what if i run this app.py file In that case, we get HeyHeyApp. So the value of name changed. And that's why this doesn't run now, this FlaskApp, because name is not equal to main anymore.

SPEAKER_00Name now is equal to app. So when this script is executed indirectly through an import statement, the value of name changes. it changes to the name of the script which happens to be app right and so in that case we make use of this as a programmer by controlling when certain lines of code should be executed so we say if this script is being imported please don't

SPEAKER_00initialize the flask application but if the script is being executed as the main script then this should run. Because when you execute the script as the main script, this variable is always equal to this string. So that is the secret behind this line. Let's make a final test. So if I rename this app.py to example.py,

SPEAKER_00and then I execute this app2.py file, you will see that we get, hey, hey, example. So the value of name changed to example, which reflects that file name. And of course, if I run this script directly, example.py, the name is always main. And therefore, this will be executed. So I hope that's clear. Thanks for following.

Open the transcript file

Hosts and Guests

Nobody is credited on this episode yet.

Credit the people on this episode

Be the first to rate this episode

More episodes

All episodes
  1. Build a Private PDF Toolkit with Python and CustomTkinter: Day 2 - Compress, Rotate and Protect PDFs, and Build an .exe

    Nov 4, 20261 minAudio

    This is a free preview of a paid episode. To hear more, visit dailypythonprojects.substack.com Yesterday we built a desktop app that merges and splits PDFs on your own computer. Today it gets three more tools: Compress,...

    Credit the people on this episode

  2. Build a Sales Report Robot with Python and Polars: Day 2 - Email the Report and Run It Every Monday

    Oct 28, 20261 minAudio

    This is a free preview of a paid episode. To hear more, visit dailypythonprojects.substack.com Yesterday we turned four Excel files into a PDF report with Polars. Today the robot sends that PDF by email, and the...

    Credit the people on this episode

  3. Read a 1 GB Log File with Python Generators: Day 2 - Generator Pipelines and a Live Log Watcher

    Oct 21, 20261 minAudio

    This is a free preview of a paid episode. To hear more, visit dailypythonprojects.substack.com Yesterday we read a 1 GB log file with one generator. Today we connect small generators into a pipeline, and point it at a...

    Credit the people on this episode

  4. How to use Daily Python Projects

    Aug 21, 20242 minAudio

    This is a public episode. If you'd like to discuss this with other subscribers or get access to bonus episodes, visit dailypythonprojects.substack.com/subscribe

    Credit the people on this episode

  5. How to Learn Python

    Aug 16, 20246 minAudio

    This is a public episode. If you'd like to discuss this with other subscribers or get access to bonus episodes, visit dailypythonprojects.substack.com/subscribe

    Credit the people on this episode