在Google Cloud上为Python中的依赖项注入添加本地存储库 [英] Adding a local repository for dependency injection in python on google cloud

查看:104
本文介绍了在Google Cloud上为Python中的依赖项注入添加本地存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Google Cloud上部署Python应用程序.在应用程序中,我有一个本地可用的依赖项.因此,要使用pip将其添加到项目中.我使用pip install -r requirements.txt --find-links PATH_TO_DEPENDENCY.这样一切都可以在本地正常工作.但是,当我使用gcloud app deploy将其部署到Google Cloud上时,它会内部调用pip install -r requirements.txt,因此未安装本地依赖项,并且代码失败.

I am trying to deploy a Python app on Google Cloud. In the app I have a dependency that is actually locally available. So to add it in project using pip. I use pip install -r requirements.txt --find-links PATH_TO_DEPENDENCY. Everything works fine this way on local. But when I deploy it on Google Cloud using gcloud app deploy it internally calls the pip install -r requirements.txt due to which the local dependency is not installed and the code fails.

是否有办法修改gcloud使用的内部命令,还是有办法告诉服务器从某处使用该依赖项?

Is there a way to modify the internal command the gcloud uses or a way to tell the server to use that dependency from somewhere?

推荐答案

尝试的一种方法是使用虚拟环境,并pip freeze捕获应用程序的所有要求,包括依赖关系:

One option to try would be to use a virtual environment and pip freeze to capture all requirements for your app, including their dependencies:

  • 拉出一个新的虚拟环境,并使用pip freeze > requirements.1.txt
  • 快照初始包内容
  • 使用本地调用来安装所有需求及其依赖项,然后使用pip freeze > requirements.2.txt
  • 获取新快照
  • 构建一个新的requirements.txt,其中包含requirements.2.txt中存在但requirements.1.txt中缺少的所有软件包(即来自原始requirements.txt或依赖项的软件包)
  • pull a fresh virtual environment and snapshot the initial package content with pip freeze > requirements.1.txt
  • use your local invocation to install all the requirements and their dependencies, then get a new snapshot with pip freeze > requirements.2.txt
  • build a new requirements.txt containing all packages present in requirements.2.txt but missing in requirements.1.txt (i.e. either from the original requirements.txt or a dependency)

然后将这个新的requirements.txt用于您的应用,应该在部署pip install -r requirements.txt期间提取所有依赖项.

Then use this new requirements.txt for your app, which should pull all dependencies during the deployment pip install -r requirements.txt.

另一个选项,有些乏味,但可用于无法通过pip安装的依赖项,它是根据相应的Google提供的docker镜像构建自定义运行时,在其中添加其他非python依赖项您的应用程序要求.来自关于自定义运行时间:

Another option, a bit more tedious but which can be used for dependencies not installable via pip, would be to build a custom runtime based on the corresponding google-supplied docker image in which you add the additional non-python dependencies your app require. From About Custom Runtimes:

自定义运行时允许您定义新的运行时环境,其中可能包括语言解释程序或应用程序服务器之类的其他组件.

Custom runtimes allow you to define new runtime environments, which might include additional components like language interpreters or application servers.

另请参见构建自定义运行时.

这篇关于在Google Cloud上为Python中的依赖项注入添加本地存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆