Search code examples
google-cloud-platformdeploymentgoogle-cloud-functionslanguagetool

Can not deploy Cloud Function with language_tool_python in Python 3.7 runtime


How you use language tool in Google Cloud Function? I'm trying to deploy in GCP Cloud Function with runtime Python 3.7. I could use the library using my local virtual environment where I have java installed.

But when I'm trying to deploy it in Cloud Function, I'm getting ModuleNotFoundError: No java install detected. Please install java to use language-tool-python.

I'm using the language_tool_python library in the cloud function

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

in the requirement.txt we have --

language-tool-python==2.4.5

I'm getting the following error message --

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. 

Can anyone please provide a solution how to use LanguageTool from server less functions?

How can we have java environment in Cloud Function along with Python 3.7?


Solution

  • The documentation prerequisite is clear, Python 3.7 and Java 8.0+ are required.

    And, with cloud functions, you can't have a custom runtime (at least, not yet), and thus you can't have Java and Python on the same runtime environment.

    For this, I recommend you to use Cloud Run. It's very similar to Cloud Function but, because you have to build your own container, you can customize the runtime environment as you want. To switch from one to the other, I wrote an article on this (my first one!)