Python 3.x 使用python子进程运行AttributeError

Python 3.x 使用python子进程运行AttributeError,python-3.x,subprocess,Python 3.x,Subprocess,Windows 7 64位上的Python 3.7.1 64位 test.py import subprocess subprocess.run(['ping localhost'], shell=True) 输出 C:\>python test.py Traceback (most recent call last): File "test.py", line 2, in <module> subprocess.run(['ping localhost'], s

Windows 7 64位上的Python 3.7.1 64位

test.py

import subprocess
subprocess.run(['ping localhost'], shell=True)
输出

C:\>python test.py
Traceback (most recent call last):
  File "test.py", line 2, in <module>
    subprocess.run(['ping localhost'], shell=True, check=True)
  File "C:\Program Files\Python37\lib\site-packages\run\__init__.py", line 145, in __new__
    process = cls.create_process(command, stdin, cwd=cwd, env=env, shell=shell)
  File "C:\Program Files\Python37\lib\site-packages\run\__init__.py", line 121, in create_process
    shlex.split(command),
  File "C:\Program Files\Python37\lib\shlex.py", line 305, in split
    return list(lex)
  File "C:\Program Files\Python37\lib\shlex.py", line 295, in __next__
    token = self.get_token()
  File "C:\Program Files\Python37\lib\shlex.py", line 105, in get_token
    raw = self.read_token()
  File "C:\Program Files\Python37\lib\shlex.py", line 136, in read_token
    nextchar = self.instream.read(1)
AttributeError: 'list' object has no attribute 'read'
C:\>python test.py
回溯(最近一次呼叫最后一次):
文件“test.py”,第2行,在
subprocess.run(['ping localhost'],shell=True,check=True)
文件“C:\Program Files\Python37\lib\site packages\run\\uuuu init\uuuu.py”,第145行,在新的__
process=cls.create_进程(命令,stdin,cwd=cwd,env=env,shell=shell)
文件“C:\Program Files\Python37\lib\site packages\run\\uuuuu init\uuuuuu.py”,第121行,在创建过程中
shlex.split(命令),
文件“C:\Program Files\Python37\lib\shlex.py”,第305行,拆分
返回列表(lex)
文件“C:\Program Files\Python37\lib\shlex.py”,第295行,下一步__
token=self.get_token()
文件“C:\Program Files\Python37\lib\shlex.py”,第105行,在get_令牌中
raw=self.read_标记()
文件“C:\Program Files\Python37\lib\shlex.py”,第136行,在read\U标记中
nextchar=self.instream.read(1)
AttributeError:“list”对象没有属性“read”
为什么会出现此错误?如何修复此错误


这并不是特定于ping命令,而是任何简单的命令,例如“dir”

经过大量的点击测试和头发拉扯,我让它工作起来了

应该是这样

subprocess.run('ping localhost', shell=True) #Remove the square brackets

无论是
'ping localhost'
(无列表)还是
shell=False
@madpysicator,您的建议都会提出同样的问题exception@BradSolomon你的建议提出了同样的例外