Python3,子进程,总是得到nonetype错误

时间:2018-11-06 16:38:55

标签: python-3.x subprocess

我是python的新手。我目前正在将python2代码移植到python 3。subprocess无法正常工作。我总是收到错误消息

  

AttributeError:'NoneType'对象没有属性'decode'

当我致电subprocess.runsubprocess.check_output等时

示例:

$python3

Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 26 2018, 19:50:54) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

import subprocess

subprocess.run(['echo','123'])

123

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 405, in run
    stdout, stderr = process.communicate(input, timeout=timeout)

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 849, in communicate
    return (stdout.decode(), stderr.decode())

AttributeError: 'NoneType' object has no attribute 'decode'

1 个答案:

答案 0 :(得分:0)

您的Python安装有问题。我使用相同版本的Python运行了相同的代码,并成功运行了该代码。

~|⇒ python3
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 26 2018, 19:50:54)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.run(['echo', '123'])
123
CompletedProcess(args=['echo', '123'], returncode=0)

尝试再次卸载并安装Python,这可能会有所帮助。