首页 > 解决方案 > 找不到python子进程命令

问题描述

RHEL7 我注意到尝试运行“应该”在环境中的任何命令都会失败,但运行具有相对或绝对路径的命令会成功。

我想我可以手动将所有这些 cal 转换为 shell 函数,/bin/whatever但如果有一种方法可以从 python 运行子进程而不必这样做,那就太好了。subprocess我缺少一些设置参数吗?

这是一堆失败的案例:

me@server~ python3 --version
Python 3.6.6
me@server~ python3
Python 3.6.6 (default, Aug 27 2018, 10:45:14)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.run('ls')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/l/python3/lib/python3.6/subprocess.py", line 403, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/l/python3/lib/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/l/python3/lib/python3.6/subprocess.py", line 1344, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'ls': 'ls'
>>> subprocess.run('ls', shell=True)
/bin/sh: ls: command not found
CompletedProcess(args='ls', returncode=127)
>>> subprocess.run(['ls'], shell=True)
/bin/sh: ls: command not found
CompletedProcess(args=['ls'], returncode=127)
>>> subprocess.run(['ls'], shell=True, executable='/bin/bash')
/bin/bash: ls: command not found
CompletedProcess(args=['ls'], returncode=127)

这是一个成功的:

>>> run('/bin/ls')
bin       ld.script.arm-bbb   ld.script.x86-galileo  Makedefs          make-errs  platform-cmake  uboot-tool  xinu      xinu.boot  xinu.x86.boot
binaries  ld.script.arm-qemu  ld.script.x86-qemu     Makedefs.EXAMPLE  Makefile   qex.exp         version     xinu.bin  xinu.map
CompletedProcess(args='/bin/ls', returncode=0)

这是我 ssh 进入的系统:

me@server~ lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-ia32:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-ia32:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-ia32:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: RedHatEnterpriseServer
Description:    Red Hat Enterprise Linux Server release 7.7 (Maipo)
Release:        7.7
Codename:       Maipo

标签: python-3.xshellpathsubprocess

解决方案


推荐阅读