Subprocess.call 或 Subprocess.Popen 不能使用 PATH (Linux/Windows) 中的可执行文件 [英] Subprocess.call or Subprocess.Popen cannot use executables that are in PATH (Linux/Windows)
问题描述
我正在编写一个程序,它需要在 Linux 和 Windows 上运行并使用路径中存在的可执行文件(带参数).(假设)
I'm writing a programme that needs to run on both Linux and Windows and use executables (with parameters) that exist in the path. (Assumed)
目前我在使用 Subprocess.Call 和 Subprocess.Popen 在 Windows 中运行可执行文件时遇到问题.
Currently I'm having trouble running executables in windows using Subprocess.Call and Subprocess.Popen.
对于这样的代码,在 windows 8 中
For a code like this, in windows 8
def makeBlastDB(inFile, inputType, dbType, title, outDir):
strProg = 'makeblastdb'
strInput = '-in ' + inFile
strInputType = '-input_type ' + inputType
strDBType = '-dbtype ' + dbType
strTitle = '-title ' + title
strOut = '-out ' + os.path.join(os.sep, outDir, title)
cmd = [strProg, strInput, strInputType, strDBType, strTitle, strOut]
result = Popen(cmd, shell=True)
我在控制台收到错误消息
I get the error message in console
'makeblastdb' is not recognized as an internal or external command,
operable program or batch file.
即使我可以使用 cmd.exe 运行相同的命令我得到与 shell=False 相同的响应.
Even though I can run the same command using cmd.exe I get the same response with shell=False.
假设可执行文件在 PATH 环境变量中,关于如何运行命令的任何想法?谢谢
Any ideas on how I can run the command assuming that the executable is in PATH environment variable? Thanks
推荐答案
好的,这就是我让它工作的方式.
Ok here is how I got it to work.
env = os.environ
proc = subprocess.Popen(args, env=env)
这篇关于Subprocess.call 或 Subprocess.Popen 不能使用 PATH (Linux/Windows) 中的可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!