Matthias
Matthias

Reputation: 5764

PyCharm: execute in Python console

I'm working on a Python project in PyCharm. For one file I would like to test parts of the code using the Python console. Problem is that the code contains a main method (used for debugging and execution) like this:

if __name__ == "__main__":
    print("with main")
else:
    print("no main")

When I execute that code in the Python Console (using context menu of PyCharm) then the first part of the if will be executed. I was expecting the else-part since I'm not starting the script using a Run-configuration.

Maybe you can help me how to do that.

Upvotes: 1

Views: 611

Answers (1)

Meroz
Meroz

Reputation: 889

Create file like not_main_start.py with only one line import main.py (I guess this is your main file). Run then this file. Output should show no main. __name__ would be name __main__ only if you call this file directly. If you use it as module then it will have name of the module.

Upvotes: 1

Related Questions