【问题标题】:gcloud function deploy with os.subprocess: [WinError 2] The system cannot find the file specifiedgcloud function deploy with os.subprocess: [WinError 2] The system cannot find the file specified
【发布时间】:2020-01-24 03:46:30
【问题描述】:

我想使用 os.subprocess.run 命令从 python 脚本部署一些 Google Cloud Functions。我可以成功构建一个语句,然后可以将其复制粘贴到我的 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:

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?

【问题讨论】:

  • 我认为您应该使用 Cloud Functions API 构建一个更具弹性的解决方案来部署函数,而不是调用 shell 并尝试在那里编写命令。

标签: python deployment subprocess google-cloud-functions gcloud


【解决方案1】:

在 Windows 上,尝试使用 gcloud.cmd 而不仅仅是 gcloud

【讨论】:

  • 这对我来说很重要!想知道为什么会这样,在我的 PATH 上有一个名为 gcloud 的二进制文件...事实上,如果我运行 where gcloud,我会同时获得 gcloudgcloud.cmd 的位置。
猜你喜欢
  • 2022-01-24
  • 1970-01-01
  • 2017-09-01
  • 2011-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-22
相关资源
最近更新 更多