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

前端 未结 4 935
眼角桃花
眼角桃花 2021-02-11 16:47

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

4条回答
  •  难免孤独
    2021-02-11 16:59

    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)

提交回复
热议问题