Search code examples
pythonpycharmconsole

Add multiple Python Console with different interpreter options


I am using a third-party Python client that can connect to different servers using some arguments.

In PyCharm I defined under Settings > Build, Execution,Deployment > Console > Python Console one of these and all works fine.

I would now like to add other Python Consoles that point to the interpreter with different interpreter options. Unfortunately the New Console button only lets me add one type.

All other Stack Overflow questions relate to different interpreters.


Solution

  • The integrated PyCharm console is a client-server that launches a new pydevconsole.py process each time you press the New Console button.

    Unfortunately the New Console button only lets me add one type.

    There are 2 ways to do this. One is simple the other is complex.

    1. The simple way.

      If you change the console settings at Settings > Build, Exection, Deployment > Console > Python Console between each time you press the New Console button then different consoles will be launched with the different interpreter options, arguments, etc, that you configured.

      The various consoles are launched with differing settings in each tab. The potential problem is that having to change the settings manually can become repetitive if you want to launch many consoles with different settings. Another problem is that if you press the Rerun button the new console will be launched using the current settings, not the ones you configured the first time you launched it.

    2. The complex way.

      Try to create a new project (for a clean example). The console settings are stored in the project's configuration file in .idea/workspace.xml. Here's an example of the default XML entity right after project creation:

       <component name="PyConsoleOptionsProvider">
        <option name="myPythonConsoleState">
          <console-settings custom-start-script="import sys; print('Python %s on %s' % (sys.version, sys.platform)); print('a test')&#10;sys.path.extend([WORKING_DIR_AND_PYTHON_PATHS])&#10;" module-name="tmp_test" is-module-sdk="true">
            <option name="myCustomStartScript" value="import sys; print('Python %s on %s' % (sys.version, sys.platform)); print('a test')&#10;sys.path.extend([WORKING_DIR_AND_PYTHON_PATHS])&#10;" />
            <option name="myUseModuleSdk" value="true" />
            <option name="myModuleName" value="tmp_test" />
          </console-settings>
        </option>
       </component>
      

      The more complex solution would be to write a Python script, configure it as an external tool, pass in the IDE's ProjectFileDir macro appending the .idea/workspace.xml path, and have that Python script change the console settings' XML element by directly editing the .idea/workspace.xml file. Finally you can bind a keyboard shortcut to the external tool.

      This would allow you to switch the console settings (cycling through your predetermined settings) with 1 keyboard shortcut, after that the New Console and Rerun buttons would work seamlessly the way you want. (I wrote a step-by-step example of this technique in this answer.)

    Both solutions can get the job done. Which one is better depends on how often you need to launch/relaunch consoles with different settings.