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

Can not deploy Cloud Function with language_tool_python in Python 3.7 runtime

你是如何使用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)

在 requirement.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清楚,需要Python3.7和Java8.0+。

并且,对于云函数,您不能拥有自定义运行时(至少,现在还不能),因此您不能在同一运行时环境中拥有 Java 和 Python .

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