Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3.6 subprocess.Popen().returncode为100意味着什么?_Python_Python 3.x_Subprocess_Ubuntu 18.04 - Fatal编程技术网

Python3.6 subprocess.Popen().returncode为100意味着什么?

Python3.6 subprocess.Popen().returncode为100意味着什么?,python,python-3.x,subprocess,ubuntu-18.04,Python,Python 3.x,Subprocess,Ubuntu 18.04,Mysubprocess.Popen()函数(见下文)返回了100returncode。这是什么意思?我找不到显示100含义的参考文献。你能给我解释一下吗?多谢各位 import subprocess def call_subprocess_Popen( cmd, cwd=None ): ''' Execute a command in BASH. kwargs: "cmd" is a list.''' with subprocess.Popen( cmd, bufsize=1

My
subprocess.Popen()
函数(见下文)返回了100
returncode
。这是什么意思?我找不到显示100含义的参考文献。你能给我解释一下吗?多谢各位

import subprocess

def call_subprocess_Popen( cmd, cwd=None ):
    ''' Execute a command in BASH. kwargs: "cmd" is a list.'''
    with subprocess.Popen( cmd, bufsize=1, universal_newlines=True, cwd=cwd,
                           stdout=subprocess.PIPE, ) as result:
        for line in result.stdout:
            print( line, end='' )
    print( 'result.returncode' )
    print( 'result.args' )
    if result.returncode != 0:
        raise subprocess.CalledProcessError( result.returncode, result.args )
    else:
        return True

def pkexec_apt_get_y_install( packages ):
    print( f'\nProcess {os.getpid()} {threading.current_thread()} pkexec apt_get_y_install ....' )
    cmd = [ 'pkexec', 'apt-get', '-y', 'install' ]
    cmd.extend( packages )
    print( f'cmd = {cmd}' )
    if call_subprocess_Popen( cmd ):
        return True
    else:
        return False

apps = [ 'synaptic', 'ubuntu-restricted-extra', 'apt-xapian-index' ]
pkexec_apt_get_y_install( apps )
错误消息:

Process 5979 <_MainThread(MainThread, started 140069990864704)> pkexec apt_get_y_install ....
cmd = ['pkexec', 'apt-get', '-y', 'install', 'synaptic', 'ubuntu-restricted-extra', 'apt-xapian-index']
Reading package lists...
Building dependency tree...
Reading state information...
result.returncode
result.args
Traceback (most recent call last):
  File "~/customiseUbuntu1804.py", line 223, in <module>
    main()
  File "~/customiseUbuntu1804.py", line 205, in main
    pkexec_apt_get_y_install( setup_apps )
  File "~/customiseUbuntu1804.py", line 76, in pkexec_apt_get_y_install
    if call_subprocess_Popen( cmd ):
  File "~/customiseUbuntu1804.py", line 47, in call_subprocess_Popen
    raise subprocess.CalledProcessError( result.returncode, result.args )
subprocess.CalledProcessError: Command '['pkexec', 'apt-get', '-y', 'install', 'synaptic', 'ubuntu-restricted-extra', 'apt-xapian-index']' returned non-zero exit status 100.
处理5979 pkexec apt_get_y_安装。。。。
cmd=['pkexec','apt get','-y','install','synaptic','ubuntu restricted extra','apt xapian index']
正在阅读软件包列表。。。
正在构建依赖关系树。。。
正在读取状态信息。。。
result.returncode
result.args
回溯(最近一次呼叫最后一次):
文件“~/customiseUbuntu1804.py”,第223行,在
main()
文件“~/customiseUbuntu1804.py”,第205行,在main中
pkexec_apt_get_y_安装(安装应用程序)
pkexec\u apt\u get\u y\u install中第76行的文件“~/customiseUbuntu1804.py”
如果调用子进程(cmd):
文件“~/customiseUbuntu1804.py”,第47行,在call\u subprocess\u Popen中
raise SUBSPROCESS.CalledProcessError(result.returncode,result.args)
subprocess.CalledProcessError:命令“['pkexec','apt get','-y','install','synaptic','ubuntu restricted extra','apt xapian index']”返回非零退出状态100。

返回代码来自外部命令,它可以表示任何内容

示例:

>>> python3 test.py 
OUT: b'This shell script will return with 100 return code\n'
ERROR: None
Return code: 100
“test.py”Python代码:

“test.sh”Shell脚本:

输出:

>>> python3 test.py 
OUT: b'This shell script will return with 100 return code\n'
ERROR: None
Return code: 100
这意味着您需要在python调用的外部命令中查找答案

注意:

>>> python3 test.py 
OUT: b'This shell script will return with 100 return code\n'
ERROR: None
Return code: 100

也许你有https来源。您可以在执行其他
apt get
命令之前尝试安装apt传输https。命令:
apt get update&&apt get install-y apt transport https

看起来您从apt get install命令中获得了100个返回代码:

E: Unable to locate package ubuntu-restricted-extra

你能提供你的源代码吗?@rusu_ro1我已经提供了源代码。从你所说的和我上面显示的代码来看,我正确理解代码100是由
apt get
返回的吗?是否有办法显示100的含义?如何重新配置代码以显示此错误消息?最好的方法是检查最后一行表单result.stdouta您可以在源代码和错误消息中看到,
result.stdout
已打印。它的最后一次打印输出是
读取状态信息…
打印('result.returncode')
接管之前。您提到的语句
E:Unable-location-package-ubuntu-restricted-extra
没有出现。我注意到,当同一个命令在终端中提交时,它确实出现了。我想知道是什么导致了这种差异?终于找到了丢失的链接。通过在源代码中为result.stderr:print(line,end='')为line.stdout:print(line,end='')为line.stderr:print(line,end='')添加
,我发现错误语句
E:not location package ubuntu restricted extra
已打印出来
subprocess.Popen().stderr
保存错误语句。此外,还需要将
stderr=subprocess.PIPE
作为
subprocess.Popen()
的一部分,即
subprocess.Popen(…,stderr=subprocess.PIPE)
,以使上述操作正常。