总的来说,我正在尝试使我的部署流程自动化,该流程包括使用maven构建和部署到Web逻辑服务器。小图片是我正在使用子进程来查看是否可以在python中调用maven。当我尝试该子过程将mvn误认为文件。 到目前为止,这是我的代码:
import subprocess
def main():
print(subprocess.check_output(["mvn", "-v"]))
if __name__ == '__main__':
main()
这是我的错误:
C:\pythondev\python.exe "C:/pythondev/development/deployment scripts/redploy-to-localhost.py"
Traceback (most recent call last):
File "C:/pythondev/development/deployment scripts/redploy-to-localhost.py", line 9, in <module>
main()
File "C:/pythondev/development/deployment scripts/redploy-to-localhost.py", line 5, in main
subprocess.check_output(["a"])
File "C:\pythondev\lib\subprocess.py", line 376, in check_output
**kwargs).stdout
File "C:\pythondev\lib\subprocess.py", line 453, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\pythondev\lib\subprocess.py", line 756, in __init__
restore_signals, start_new_session)
File "C:\pythondev\lib\subprocess.py", line 1155, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
Process finished with exit code 1
尽管我的问题在于子流程,但我乐于接受提出更好替代方案的答案。
答案 0 :(得分:1)
我遇到了同样的问题,由于互联网告诉我这很邪恶,所以一直不愿意使用shell=True
。
当我在cmd.exe中运行where mvn
时,我看到有两个匹配项:
mvn
,它是Unix shell脚本(以#!/bin/sh
开头),mvn.cmd
,这是Windows批处理文件。 我认为在mvn something -something
中执行cmd.exe
时会发生以下情况:Windows尝试查找名为mvn
的可执行文件。它找到mvn
文件,但意识到该文件不可执行。然后,它尝试查找诸如mvn.com
,mvn.exe
,...之类的文件(请参见%PATHEXT%
系统变量)。找到mvn.cmd
后,它将执行该命令,每个人都会感到高兴。
据我了解,subprocess.check_output
(和subprocess.run
等)的问题在于,未执行通过%PATHEXT%
的路径“扩展”。因此,解决方案是您必须手动指定扩展名并以以下方式运行命令
print(subprocess.check_output(["mvn.cmd", "-v"]))
答案 1 :(得分:0)
尝试一下对我有用。
print(subprocess.check_output(["mvn", "-v"], shell=True))