我有一些Google Cloud Functions,我想使用os.subprocess.run命令从python脚本中进行部署。我可以成功构建一条语句,然后将其复制粘贴到我的Window终端中以部署看起来像这样的函数:
gcloud functions deploy <cloud_function_name> --runtime python37 --trigger-http --entry-point <function_in_main_to_call> --project <project_id> --timeout 540 --source "C:\\Path\\to\\code base with spaces\\project_dir"
注意上面的命令在代码所在的本地目录的路径中有空格。
我知道subprocess.run需要一个参数列表,其中第一个是要执行的命令。这是我用来将上面的命令(在终端中运行)变成subprocess.run的参数列表的代码:
deploys = [
('gcloud functions deploy <cloud_function_name> --runtime python37 '
'--trigger-http --entry-point <function_in_main_to_call> '
'--project {} --timeout 540 '
'--source "{}"'.format(PROJECT_ID, 'C:\\Path\\to\\code base with spaces\\project_dir')),
]
for d in deploys:
beg, source = d.rsplit(' --source ', 1)
args = beg.split(' ') + ['--source'] + [source.replace('"', '\"')]
subprocess.run(args=args)
args变量中的额外列表/字符串操作是尝试解决该错误。当在上面调用subprocess.run时,我得到以下回溯:
Traceback (most recent call last):
File "<input>", line 9, in <module>
File "C:\miniconda3\envs\myenv\lib\subprocess.py", line 403, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\miniconda3\envs\myenv\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "C:\miniconda3\envs\myenv\lib\subprocess.py", line 990, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
我已验证路径/文件存在。我假设它与--source
路径字符串的格式有关,但我还没有找到解决方案。如何通过此python脚本部署Google Cloud Functions?
在Windows上,尝试gcloud.cmd
,而不只是gcloud
。