Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run a shell script instead of python in a PyCharm run configuration?

Tags:

pycharm

I am trying to setup PyCharm to invoke a shell script, instead of python, as the run option. Is this possible? In the default options I only have Python, Python docs, and Python tests. With the professional edition I also have Django and others. However, they all have python as the interpreter in a combobox and they can't be changed as far as I can see.

like image 685
Stefano Borini Avatar asked Jun 15 '15 09:06

Stefano Borini


People also ask

How do I run a shell script in PyCharm?

Run/Debug Configuration: Shell Script Use this dialog to configure running shell scripts. The set of parameters in the dialog depends on the option you select under Execute: Script file (a configuration to run a script file)

How do I change the run configuration in PyCharm?

From the main menu, select Run | Edit Configurations. Alternatively, press Alt+Shift+F10 , then 0 . In the left-hand pane of the run/debug configuration dialog, click Edit configuration templates…. In the Run/Debug Configuration Templates dialog that opens, select a configuration type.

Can Python replace shell script?

Instead, the Python programming language can be used as a very able replacement. There are many benefits to using Python as a replacement for shell scripts: Python is installed by default on all the major Linux distributions. Opening a command line and typing python immediately will drop you into a Python interpreter.


2 Answers

If you want to see such a feature in PyCharm please vote on IDEA-112256 'Command Line' Run Configuration feature request.


Run/Debug Configurations section of Pycharm's online help lists all supported types of Run/Debug configurations and there's no support for shell scripts indeed. However, you might be able to add such support by installing a plugin. For example, if you are interested in bash scripts there's BashSupport plugin which adds support for running shell scripts in Run/Debug configuration.

From plugins' home page:

BashSupports can directly run scripts within IntelliJ. You can create a new run configuration for Bash scripts. Here you can set which interpreter is used to run it. Whenever a script is executed the output is logged. If Bash prints out syntax errors then the errorneous lines are clickable to jump to the location of the error.

For Windows there's CmdSupport plugin which provides an action to run .cmd scripts. It seems it does not support running such scripts as Run/Debug configuration however.

As a workaround you can use Python run/debug configuration, specifying some dummy (empty) Python file to run and use Before launch option specifying External tool and specify path to the script when adding/configuring this external tool. See In IntelliJ IDEA, how can I create a key binding that executes a shell script with the current file as a parameter? for details.

As PyCharm is based on IntelliJ's IDEA platform the question IntelliJ IDEA: Running a shell script as a Run/Debug Configuration is very related.


Speaking of run/debug configurations you might be interested in the plugin Run Configuration as Action which

(...) provides a way to use run configurations as buttons on toolbar. Or assign shortcuts to execute specific run configuration.

like image 129
Piotr Dobrogost Avatar answered Oct 13 '22 02:10

Piotr Dobrogost


This is really a missing feature that normally should be considered as basic functionality. There are two options

  1. First i tried to create a standard (empty) Python configuration and used the "Before launch"->External tool option, therefore you must create a new external tool definition with Tool Settings:

    • Program: cmd.exe
    • Parameters: /C your-batch-file.bat
    • Working directory $ProjectFileDir$ (In my case $ProjectPath$ was empty)

    The annoying thing about this solution is, that the "external tool" standard output is redirected to an extra tab in the console log window which is immediately going into the background when the dummy Python Configuration is executed afterwards.

  2. Second and better is to use python to execute the command. Normally i always use subprocess module but in this case os.system() is the nice and minimal solution. The python run configuration then looks like this
    • Script: (empty)
    • Parameters: -c "import os; os.system('your-batch-file')"
    • Working directory: (select project directory, unfortunately no macros here)
like image 41
user6316071 Avatar answered Oct 13 '22 01:10

user6316071