subprocess.check_output给出了windows错误

时间:2018-05-03 10:23:16

标签: python python-2.7 subprocess

我试图运行用python编写的名为develop.py的文件。但它给出了错误。这是我的代码:

import base64
import getpass
import os
import subprocess
import sys

credentials = subprocess.check_output([
    'openssl',
    'aes-256-cbc',
    '-d',
    '-in',
    'credentials.development'
    ], shell=True)

env = os.environ.copy()
env['CREDENTIALS'] = credentials
env['CONFIG'] = 'config.yaml.development'
env['ENVIRONMENT'] = 'development'

subprocess.check_call(['grunt', 'web:develop'], env=env)

以下是运行文件时的结果:

 E:\QA\QA>python develop.py
    'openssl' is not recognized as an internal or external command,
    operable program or batch file.
    Traceback (most recent call last):
        File "develop.py", line 13, in <module>
           ], shell=True)
        File "C:\Python27\lib\subprocess.py", line 219, in check_output
            raise CalledProcessError(retcode, cmd, output=output)
        subprocess.CalledProcessError: Command '['openssl', 'aes-256-cbc', 
 '-d', '-in', 'credentials.development']' returned non-zero exit status 1

环境:
Windows 10(64位)
Python 2.7

如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

我认为这是一个路径问题,而不是Python问题。当Windows无法找到您所使用的命令时,会发现上述错误。

在这种情况下,命令openssl

如果openssl未添加到path或未指定文件路径,则会产生此错误。

因此,您应该检查路径是否已添加到path环境变量中,如果没有,则添加它。

答案 1 :(得分:0)

这意味着 其中一个Commandes:

'openssl',
'aes-256-cbc',
'-d',
'-in',
'credentials.development'

不要存在于openssl中

但如果你试试这个:

import base64
import getpass
import os
import subprocess
import sys


credentials = subprocess.check_output(['dir'], shell=True)
env = os.environ.copy()
env['CREDENTIALS'] = credentials
env['CONFIG'] = 'config.yaml.development'
env['ENVIRONMENT'] = 'development'
subprocess.check_call(['grunt', 'web:develop'], env=env)

用dir,它将完美地工作bcz命令存在