无法在Python 3.7运行时中使用language_tool_python部署Cloud Function

问题描述

您如何在Google Cloud Function中使用语言工具?我正在尝试使用运行时Python 3.7在GCP Cloud Function中进行部署。我可以在安装Java的本地虚拟环境中使用该库。

但是当我尝试在Cloud Function中部署它时,我得到了 ModuleNotFoundError:未检测到Java安装。请安装Java以使用language-tool-python。

我正在使用云函数中的language_tool_python库

# install and import for grammar accuracy
import language_tool_python
tool = language_tool_python.languagetool('en-IN')
matches = tool.check(input_string)

在require.txt中,我们有-

language-tool-python==2.4.5

我收到以下错误消息-

Function Failed on loading user code. Error message: Code in file main.py can't be loaded. Did you list all required modules in requirements.txt? Detailed stack trace: Traceback (most recent call last):
 File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py",line 359,in check_or_load_user_function _function_handler.load_user_function() 
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py",line 236,in load_user_function spec.loader.exec_module(main_module) File "<frozen importlib._bootstrap_external>",line 728,in exec_module File "<frozen importlib._bootstrap>",line 219,in _call_with_frames_removed
File "/user_code/main.py",line 10,in <module> from libraries.unkNown_word import word_meaning File "/user_code/libraries/unkNown_word.py",line 18,in <module> tool = language_tool_python.languagetool('en-IN') 
File "/env/local/lib/python3.7/site-packages/language_tool_python/server.py",line 46,in __init__ self._start_server_on_free_port() File "/env/local/lib/python3.7/site-packages/language_tool_python/server.py",line 183,in _start_server_on_free_port self._start_local_server() 
File "/env/local/lib/python3.7/site-packages/language_tool_python/server.py",line 193,in _start_local_server download_lt() File "/env/local/lib/python3.7/site-packages/language_tool_python/download_lt.py",line 144,in download_lt confirm_java_compatibility() 
File "/env/local/lib/python3.7/site-packages/language_tool_python/download_lt.py",line 75,in confirm_java_compatibility 
raise ModuleNotFoundError('No java install detected. Please install java to use language-tool-python.') 
ModuleNotFoundError: No java install detected. Please install java to use language-tool-python. 

任何人都可以提供一种解决方案,该方法如何从服务器较少的功能中使用languagetool

我们如何在Cloud Function中将Java环境与Python 3.7一起使用?

解决方法

documentation prerequisite很清楚,需要Python 3.7和Java 8.0 +。

而且,有了云功能,您将无法(至少现在还没有)自定义运行时,因此,您不能在同一运行时环境中拥有Java和Python。

为此,建议您使用Cloud Run。它与Cloud Function非常相似,但是由于您必须构建自己的容器,因此可以根据需要自定义运行时环境。 要从一个切换到另一个,我wrote an article on this (my first one!)