0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Azure Functions with Pythonの落とし穴

Last updated at Posted at 2021-09-30

経緯

自作ライブラリを.python_packages/lib/site-packagesにインストールする方法が紹介されていた。
非公開のPythonライブラリを使用したかったので、この方法を試したが憂き目にあった。

環境

以下の環境で進めた。Pythonは3.7.9とする。

> func --version
3.0.3568
>pip list | findstr azure-cli
azure-cli                               2.28.0
azure-cli-core                          2.28.0
azure-cli-telemetry                     1.0.6

事象:Numpyがビルドされない

Azure Functions AppにPython製のプログラムをfunc azure functionapp publish <MY_APP_NAME> --no-buildしたらnumpyがうまくビルドされなかった。上述.python_packagesを使う方法のときは、publishの際に--no-buildオプションをつけろと指示があったが、--no-buildしてしまうとnumpyがうまく入らない。(numpyはC言語で一部書かれているので、おそらく実行環境中のCコンパイラでビルドしないとうまく動作しないのだろう)

Result: Failure Exception: ImportError: IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE! Importing the numpy C-extensions failed. This error can happen for many reasons, often due to issues with your setup or how NumPy was installed.
We have compiled some common reasons and troubleshooting tips at: https://numpy.org/devdocs/user/troubleshooting-importerror.html Please note and check the following: 
The Python version is: Python3.7 from "/usr/local/bin/python" * The NumPy version is: 
"1.21.2" and make sure that they are the versions you expect. Please carefully study the documentation linked above for further help. Original error was: No module named 'numpy.core._multiarray_umath' . 
Troubleshooting Guide: https://aka.ms/functions-modulenotfound Stack:
  File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/dispatcher.py", line 309, in _handle__function_load_request func_request.metadata.entry_point)
  File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 42, in call raise extend_exception_message(e, message)
  File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 40, in call return func(*args, **kwargs)
  File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/loader.py", line 83, in load_function mod = importlib.import_module(fullmodname) 
  File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) 
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import 
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load 
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked 
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/site/wwwroot/HttpTrigger/__init__.py", line 2, in <module> import tabula
  File "/home/site/wwwroot/.python_packages/lib/site-packages/tabula/__init__.py", line 3, in <module> from .io import convert_into, convert_into_by_batch, read_pdf, read_pdf_with_template 
  File "/home/site/wwwroot/.python_packages/lib/site-packages/tabula/io.py", line 30, in <module> import numpy as np 
  File "/home/site/wwwroot/.python_packages/lib/site-packages/numpy/__init__.py", line 150, in <module> from . import core File "/home/site/wwwroot/.python_packages/lib/site-packages/numpy/core/__init__.py", line 48, in <module> raise ImportError(msg)

解決策

フォルダにライブラリをフォルダとして保存する。ドキュメントでは以下のようにdependencies.dependency1モジュールを追加する例が書いてある。この方法で無事に自作ライブラリをAzure Functions内で読み込むことができた。

筆者一部改変
<project_root>/
 | - my_first_function/
 | | - __init__.py
 | | - function.json
 | | - example.py
 | - dependencies/
 | | - dependency1.py
 | - .funcignore
 | - host.json
 | - local.settings.json
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?