Pythonサブプロセスで 'command'コマンドが見つかりません。

Pythonサブプロセスで 'command'コマンドが見つかりません。

Pythonスクリプトで次のコマンドを使用できませcommandんでした。

import subprocess

subprocess.run(["command", "-v", "yes"])

そして次に続く

Traceback (most recent call last):
  File "command_test.py", line 3, in <module>
    subprocess.run(["command", "-v", "yes"])
  File "/usr/lib/python3.5/subprocess.py", line 383, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/usr/lib/python3.5/subprocess.py", line 676, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.5/subprocess.py", line 1282, in _execute_child
    raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'command'

シェル(zsh)は期待どおりに機能します。

$ command -v yes         
/usr/bin/yes

commandPythonサブプロセスでどのように使用しますか?いくつかの追加パッケージをインストールする必要がありますか?

環境:

Python 3.5.3とzsh 5.3.1を含むDebian 9(拡張)

ベストアンサー1

commandこれはシェルに組み込まれたコマンドなので、ファイルシステム内の独自のオブジェクトではありません。

man bash/man zshまたはを参照してくださいhelp command

$ python3 -c 'import subprocess ; subprocess.run(["bash","-c","command -v yes"])'
/usr/bin/yes

解決策になる可能性があります(私はzshインストールしていないので、私の例ではそれを使用していますbash)。

おすすめ記事