Pycharm won't allow to run a file. Shows run unittest option only.

前端 未结 6 1382
猫巷女王i
猫巷女王i 2020-12-28 12:41

I have a file and it has bunch of methods starting with prefix \"test\". Now I want to run this file and it has a __name__ == \"__main__\" construct which sets

相关标签:
6条回答
  • 2020-12-28 12:56

    One detail missing from @yole’s answer is that you need to delete any existing configurations associated with the file before you can right-click and get it to run/debug. So, the full procedure is:

    1. Delete any existing configurations for that file by going to ‘Run -> Edit Configurations’ in the menu.
    2. Right click inside the if __name__ == '__main__': block
    3. Choose Run
    4. Save the configuration

    After that, you should be able to run or debug the file as expected.

    0 讨论(0)
  • 2020-12-28 12:58

    Using PyCharm Community Edition 2017.2 EAP. Here is how I did it:

    1. From the Run menu select Run.... (Or use its keyboard shortcut)
    2. A popup menu appears that should contain at least three entries: edit configuration..., my_script_name, and Unittests in my_script_name.py. Click on the right arrow of my_script_name.
    3. If the configuration is not saved, you should be able to see a save configuration option in the arrow menu. Choose it.

    If the configuration changed again, follow the same steps but this time choose run in the last step.

    0 讨论(0)
  • 2020-12-28 13:11

    I couldn't get @yole's answer to work but i did manage by going to run > edit configurations > new configuration (the plus sign) > select python, now input a name (mine was 'stop the bloody unittest') and select the correct script

    0 讨论(0)
  • 2020-12-28 13:12

    If you right-click inside the 'if name == "main"' block, it will show you the regular "Run script" option instead of "Run unit test". After that, you can save the created run configuration and use it to run the script.

    0 讨论(0)
  • 2020-12-28 13:15

    As to me, "If you right-click inside the 'if name == "main"' block" will not work.

    For example if you want to run "dlib_fun_test.py", you should ‘Edit Configurations’ , remove it from "python tests", and then add it to the above "Python".

    The below is my configure:

    Or you can also run the script, in bash console by python just_test.py.

    0 讨论(0)
  • 2020-12-28 13:18

    I had the same problem, deleted my .idea folder in the directory of the project I was working on and was able to run my tests normally again:

    PyCharm 2018.2.4 (Community Edition)
    Build #PC-182.4505.26, built on September 19, 2018
    JRE: 1.8.0_152-release-1248-b8 amd64
    JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
    Windows 10 10.0
    

    P.S. You can your version of PyCharm by going to Help -> About

    0 讨论(0)
提交回复
热议问题