Qiang Li
Qiang Li

Reputation: 10855

execute main through if condition in python

I have a python script in which there is a line:

if __name__ == "__main__":
    ...

So how can I run this portion of the code in another script after importing the script above?

Thanks.

Upvotes: 0

Views: 86

Answers (1)

Sean Redmond
Sean Redmond

Reputation: 4114

Make it an independent function.

def run_main():
    ....

if __name__ == "__main__":
    run_main()

And you can call run_main() from another file.

Upvotes: 10

Related Questions