简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM