2

I want to run shell scripts and git-bash commands using subprocess.run(), in python 3.7.4. When I run the simple example on the subprocess documentation page this happens:

import subprocess

subprocess.run(["ls", "-l"])

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\pycharm\project\envs\lib\subprocess.py", line 472, in run
    with Popen(*popenargs, **kwargs) as process:
  File "C:\pycharm\project\envs\lib\subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "C:\pycharm\project\envs\lib\subprocess.py", line 1178, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified


# it also fails with shell=True
subprocess.call(["ls", "-l"], shell=True)

'ls' is not recognized as an internal or external command,
operable program or batch file.
1

The message from shell=True is a message from windows cmd, which suggests subprocess is not sending commands to git-bash.

I am using a conda environment located in the project/envs/ folder for python. I have also installed git-bash.

I also tried setting the env and got the same error.

import os
import subprocess

my_env = os.environ.copy()
my_env["PATH"] = 'C:\Program Files\Git\;' + my_env["PATH"]
subprocess.run(['git-bash.exe', 'ls', '-l'], env=my_env)

Traceback (most recent call last):
  File "<input>", line 3, in <module>
  File "C:\pycharm\project\envs\lib\subprocess.py", line 472, in run
    with Popen(*popenargs, **kwargs) as process:
  File "C:\pycharm\project\envs\lib\subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "C:n\pycharm\project\envs\lib\subprocess.py", line 1178, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

I can get it to run by pointing at the git-bash.exe, but it returns an empty string instead of the files in my directory

import subprocess
subprocess.run(['C:\Program Files\Git\git-bash.exe', 'ls', '-l'], capture_output=True)

CompletedProcess(args=['C:\\Program Files\\Git\\git-bash.exe', 'ls', '-l'], returncode=0, stdout=b'', stderr=b'')


I would appreciate any advice on the best way to get this working as shown on the subprocess documentation page.

rok
  • 9,403
  • 17
  • 70
  • 126
Tony B
  • 376
  • 1
  • 3
  • 12
  • if you run `ls -l` in windows cmd, does it work? if you run `C:\Program Files\Git\git-bash.exe ls -l` in windows cmd, does it work? if they don't, those commands won't run via Python either. Try to run some command in windows cmd first, succeed, then run it via Python. – rok Oct 15 '19 at 22:26
  • In cmd `ls -l` returns `'ls' is not recognized as an internal or external command, operable program or batch file,` just like `subprocess.call(["ls", "-l"], shell=True)`. And running `C:\Program Files\Git\git-bash.exe ls -l` opens a git bash terminal which then closes quickly, without printing results from `ls`, or maybe it is just closing immediately so I can't see the printing. if I change to `C:\Program Files\Git\git-bash.exe sleep 5`, the git terminal does not stay open for even 1 second, suggesting it is not running the command. – Tony B Oct 16 '19 at 14:41

4 Answers4

3

I found that I can run commands using ...Git\bin\bash.exe instead of the ...\Git\git-bash.exe, like this:

import subprocess
subprocess.run(['C:\Program Files\Git\\bin\\bash.exe', '-c','ls'], stdout=subprocess.PIPE)

CompletedProcess(args=['C:\\Program Files\\Git\\bin\\bash.exe', '-c', 'ls'], returncode=0, stdout=b'README.md\n__pycache__\nconda_create.sh\nenvs\nmain.py\ntest.sh\nzipped\n')

Tony B
  • 376
  • 1
  • 3
  • 12
  • Possible alternative solution: if it's not a bash built-in, it might be invocable with its Windows path (such as `r'C:\Program Files\Git\bin\ssh.exe'`) (not tested!) – user202729 Aug 16 '21 at 13:58
  • This is a crude workaround; the proper solution is to use the `executable` keyword argument to tell `subprocess` which shell to use. – tripleee Sep 11 '21 at 09:10
0

Try this

p = subprocess.Popen(("ls", "-l"), stdout=subprocess.PIPE)
nodes = subprocess.check_output(("grep"), stdin=p.stdout)
p.wait()
ajmartin
  • 2,379
  • 2
  • 26
  • 42
  • When I try to define p I get the same FileNotFoundError, but it starts at line 775 because run just calls popen – Tony B Oct 15 '19 at 21:39
  • Print the output and see the directory being used at the time of `ls` using `subprocess`. – ajmartin Oct 15 '19 at 21:41
  • Since `ls` doesn't seem to be running, there is no output to print. I can run `subprocess.run(['dir'], shell=True)` and it shows `Directory of C:\pycharm\project` – Tony B Oct 16 '19 at 14:48
0
  • ls is Linux shell command for listing files and directories
  • dir is Windows command line command for listing files and directories

Try to run dir in Windows command line. If it works, try to run the same command using python subprocess:

import subprocess

subprocess.run(["dir"])
rok
  • 9,403
  • 17
  • 70
  • 126
  • I get the same `FileNotFoundError: [WinError 2] . . .`. But when I run `subprocess.run(['dir'], shell=True)` the results of running `dir` in cmd print. However, I want to use linux commands so that I can also run the code on linux servers. – Tony B Oct 16 '19 at 14:46
-1

For a machine with Windows Operating System, Try the following

import subprocess
subprocess.run(["dir", "/p"], shell=True)

"ls" is replaced with "dir", "-l" is replaced with "/l" and the "shell" is set to true

For a machine with Linux/Mac Operating System, Try the following

import subprocess
subprocess.run(["ls", "-l"])
Sunny
  • 2,183
  • 1
  • 17
  • 13