2

I would like to modify the default run (not debug) behavior in Pycharm to include the -O flag, as indicated in this answer.

The information in this answer on creating a run configuration is helpful, but I have many files with if __name__ == "__main__" blocks that I need to run locally, and I would like Pycharm to automatically execute them with the -O option during a "run". The debug behavior should remain unchanged and the -O flag shouldn't be set.

Dorian Turba
  • 3,260
  • 3
  • 23
  • 67
safex
  • 2,398
  • 17
  • 40

1 Answers1

1

-O for run but not Debug

You can't do that with configuration but:

Use External tool Pycharm feature

You can configure an external tool configuration (settings>Tools>"External Tools">Add) like this: edit external tool pycharm panel

Then, you can assign a shortcut in keymap to run this configuration that will run Python with optimization flag on the current file. keymap setting panel

-O for default configuration (both run/debug)

Use Run/Debug configurations>Edit configuration templates

You can't choose a configuration based on the content of a file (or not easily) or change options if it is run or debug, but you can change the default template used for configuration to include the -O flag in the interpreter options.

Pycharm run/debug configuration template panel

Dorian Turba
  • 3,260
  • 3
  • 23
  • 67