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

Robert Parson
Robert Parson
6,365 Points

__name__ = "__main__"

Are you able to change "main" to another file, so the code will only work within another file. Without running in its original file its programmed into, yes or no? And if yes how?

2 Answers

Travis Alstrand
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Travis Alstrand
Treehouse Project Reviewer

I hope I understand the question correctly, but here goes 😬

You can change the __name__ attribute to restrict the execution of a Python script to only occur when it is imported into another file. This is usually done to prevent a script from running when it's imported as a module.

You can achieve this by modifying the __name__ attribute within the script you want to control like so...

script.py

def main_function():
    print("This is the main function.")

if __name__ == "__main__":
    main_function()

If you want script.py to only execute when it is imported into another file, you can change it like so...

script.py

def main_function():
    print("This is the main function.")

if __name__ == "__script__":
    main_function()

Then, in another file, you could import script.py like so...

other_script.py

import script

Here, the main_function() in script.py won't run automatically when script.py is imported into other_script.py. It will only run if explicitly called within other_script.py or any other file where it's imported.

I hope this answers your question.

Robert Parson
Robert Parson
6,365 Points

Last question are you able to set it to a specific file? And yes that's what I was wanting to know thanks so much!

Rachel Johnson
STAFF
Rachel Johnson
Treehouse Teacher

Hey Robert Parson , thanks for your question! If I read correctly, I believe you're asking if __main__ can refer to another file.

__name__ refers to the file that Python is currently running, and __main__ refers to the file that the user asked Python to run initially. So, while we can change __main__ to something else, all we'll be doing really is checking if the current file is the same as the current file (or another file, which will always equate to false). q I guess to best answer your question, we'll need to know your use case. Why are you wanting to check for the file name?

Some resources on __name__ that might help:

Robert Parson
Robert Parson
6,365 Points

Just to be able to know if I can lol, I like to learn as much as I can about things I'm using often