Adding a local repository for dependency injection in python on google cloud

半腔热情 提交于 2019-12-20 03:34:18

问题


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.

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?


回答1:


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

  • 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)

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

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.

See also Building Custom Runtimes.



来源:https://stackoverflow.com/questions/50627798/adding-a-local-repository-for-dependency-injection-in-python-on-google-cloud

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!