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

When we run the python second_app.py, what the "app" works here and why print_hello() from the app.py?

#app.py
def print_hello():
  print("hello from app.")

print(__name__)

if __name__ == '__main__':
  print_hello()



#second_app.py
import app
print("hello from second_app.")
app.print_hello()
#console
python app.py
__main__
hello from app.


python second_app.py
app                 - what is this for???
hello from second_app.
hello from app.                - why it is it here?

what the "app" works on the python second_app.py? since the app.py has a conditional statement

if __name__ == '__main__':
  print_hello()

why print_hello() from the app.py from the python second_app.py?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

Hey Hanwen Zhang, good question! The print(__name__) is being used to show how the variable __name__ changes depending if the current module is the top level (called directly using python), or if the current module was imported into another module.

When running python app.py, the __name__ is changed to "__main__" to indicate the file was called directly. Therefore, any code within the if __name__ == "__main__" block will be executed. A call is made to the local function print_hello() to cause something to happen. Otherwise, only the print(__name__) statement would execute.

When running python second_app.py, the following occurs:

  • The first statement imports app.py, which causes all statements in app.py to be executed and the functions to be put in the app namespace.
    • The function print_hello would be reference as app.print_hello() within second_app code.
    • The print statement would be executed. Since app.py was imported, __name__ keeps the module name "app".
    • All other execution during import is block by the if __name__ == "__main__" statement
  • The statement `print("hello from second_app.") is executed.
  • The statement app.print_hello() runs, referencing the function in the imported file.
    • This causes print("hello from app.") to be executed.

Basically, these two files demonstrate how the value of __name__ within a module changes if a module is directly executed and how the value retains the module name if a module is imported.

Post back if you need more help. Good luck!!

Hi Chris, thank you! You explained it very well, I understood now.