Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Conditional Check

Ritu tin
PLUS
Ritu tin
Courses Plus Student 1,461 Points

What if I import two module then what will the value of __name__ in that case will it be equal to second module???

What if I import two module in my app.py file then what will the value of name in that case will it be equal to second module which will be imported .

2 Answers

Josh Keenan
Josh Keenan
20,315 Points

__name__ isn't something being saved in your main project file, you can have a million imports using this and they will all work, I would recommend going over the video again as you have misunderstood it.

App.py

def my_function():
    print("Hey from my_function")

if __name__ == "__main__":
    my_function()

In this example, when you import this file to another you can now access the functions, classes, and anything else in the file. In the video, without the final 2 lines of code, you can see that the function would just run anyway.

These lines are telling the code that this should not be run unless explicitly told to.

another_app.py

import app
print("Here's something")

app.my_function()

Now if we remove the if __name__ == "__main__": section then the code just runs, it allows us to import our own code and use it as and when we please, not just as soon as the file is executed.

Feel free to ask any further questions.