Skip to content
Discussion options

You must be logged in to vote

Why run_name="__main__": runpy.run_path executes the file in a fresh namespace, and by default sets that namespace's __name__ to the path string ("answer.py" or similar) — not "__main__". That means any if __name__ == "__main__": guard in the target file would never fire, and scripts that rely on the guard (plus anything that checks __main__ semantics) would print nothing. Passing run_name="__main__" makes the file behave exactly as if you'd typed python answers/exercise4/answer.py — which is the whole point of a runner. (One side effect to know: if the target file doesn't guard its top-level prints, they run either way — run_name only matters for guarded code and __main__ checks.)

Should…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by dab00gieman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants