Search code examples
pythonpycharmsubprocessjetbrains-ideexecv

How to have a script open another script in a way that interacts well with other features of a JetBrains-IDE?


I have a script that is basically just a small GUI to create a config-file for a long-running-program without any graphics. I have and control the source-code for both of these.

The usual workflow of the developer-users is to open that GUI using a simple python-run-configuration, tweak some settings,and click its 'run'-button to launch the script.

This works, but the 'run'-window closes and the process appears in a shell, losing all nice features of the 'run'-window such as clickable output and the debugger UI.

I have tried two methods of starting the process: subprocess.Popen and os.execv. I have also tried to set the Pycharm-run-configuration-option Execution>Emulate terminal in output console to ticked and unticked.

I also have two alternatives that I like to avoid: use the integrated terminal (key-users don't like it, and a separate run-configuration for the main-program would be required) or make the script-launching a function call without any processes involved (requires some refactoring and care with a singleton-state)

Is there a non-hacky way to have a script like my GUI be usable from within pycharm?


Solution

  • I have found a way to launch run_gui.py from pycharm in a way that lets me use the pycharm run-window on run_calc.py.

    1. Make a run-configuration that executes run_calc.py.
    2. In this configuration, add the task that runs run_gui.py in the "Before launch"-window. This task will execute before the actual script and its return-value decides if the actual task runs.
    3. change the source-code of run_gui.py to not launch run_calc.py directly in any way but instead call sys.exit(0) if the calculation should run or sys.exit(1) if it should not run.

    I am willing to accept another answer if it can do it with less configuration and without abusing returncodes.