简体   繁体   中英

Cannot import pyodbc insisde an azure function but an import in regular python file works (using the same venv in both)

I have followed official tutorial for installing ODBC driver and installed pydobc with exporting the flags and updating odbc files with drivers locatios. Hence I think managed to install it correctly.

I have a python venv where I pip installed pyodbc and with success I can

python3.9 -m venv .venv
source .venv/bin/activate
pip install pyodbc
> installed 4.0.32
>python
>import pyodbc

When I try to host a default code azure function it's a success. But when I add import pyodbc at the top of the file, it fails.

Expected behaviour:

  • Function should run and import pyodbc

Actual behaviour:

  • It fails, stack trace below.

Specs:

  • Python: 3.9.13
  • Core Tools Version 3.0.4585 Commit hash: N/A (64-bit)
  • Function Runtime Version: 3.7.1.0
  • Mac os M1 12.4
For detailed output, run func with --verbose flag.
[2022-07-09T15:45:22.308Z] Traceback (most recent call last):
[2022-07-09T15:45:22.314Z]   File "/opt/homebrew/Cellar/azure-functions-core-tools@3/3.0.4585/workers/python/3.9/OSX/X64/azure_functions_worker/bindings/shared_memory_data_transfer/file_accessor_unix.py", line 127, in _get_valid_mem_map_dirs
[2022-07-09T15:45:22.315Z]     os.makedirs(dir_path)
[2022-07-09T15:45:22.315Z]   File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/os.py", line 215, in makedirs
[2022-07-09T15:45:22.315Z]     makedirs(head, exist_ok=exist_ok)
[2022-07-09T15:45:22.315Z]   File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/os.py", line 225, in makedirs
[2022-07-09T15:45:22.315Z]     mkdir(name, mode)
[2022-07-09T15:45:22.315Z] PermissionError: [Errno 1] Operation not permitted: '/dev/shm'
[2022-07-09T15:45:22.629Z] Worker process started and initialized.
[2022-07-09T15:45:23.001Z] Worker failed to function id 716b2b93-8695-4822-ad71-cd76c3035b3c.
[2022-07-09T15:45:23.006Z] Result: Failure
[2022-07-09T15:45:23.006Z] Exception: ImportError: dlopen(/Users/bartoszlachowicz/Desktop/CodeProjects/customers-slack-app/.venv/lib/python3.9/site-packages/pyodbc.cpython-39-darwin.so, 0x0002): symbol not found in flat namespace '_SQLAllocHandle'. Troubleshooting Guide: https://aka.ms/functions-modulenotfound
[2022-07-09T15:45:23.006Z] Stack:   File "/opt/homebrew/Cellar/azure-functions-core-tools@3/3.0.4585/workers/python/3.9/OSX/X64/azure_functions_worker/dispatcher.py", line 305, in _handle__function_load_request
[2022-07-09T15:45:23.006Z]     func = loader.load_function(
[2022-07-09T15:45:23.006Z]   File "/opt/homebrew/Cellar/azure-functions-core-tools@3/3.0.4585/workers/python/3.9/OSX/X64/azure_functions_worker/utils/wrappers.py", line 42, in call
[2022-07-09T15:45:23.006Z]     raise extend_exception_message(e, message)
[2022-07-09T15:45:23.006Z]   File "/opt/homebrew/Cellar/azure-functions-core-tools@3/3.0.4585/workers/python/3.9/OSX/X64/azure_functions_worker/utils/wrappers.py", line 40, in call
[2022-07-09T15:45:23.006Z]     return func(*args, **kwargs)
[2022-07-09T15:45:23.006Z]   File "/opt/homebrew/Cellar/azure-functions-core-tools@3/3.0.4585/workers/python/3.9/OSX/X64/azure_functions_worker/loader.py", line 85, in load_function
[2022-07-09T15:45:23.006Z]     mod = importlib.import_module(fullmodname)
[2022-07-09T15:45:23.006Z]   File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
[2022-07-09T15:45:23.006Z]     return _bootstrap._gcd_import(name[level:], package, level)
[2022-07-09T15:45:23.006Z]   File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
[2022-07-09T15:45:23.006Z]   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
[2022-07-09T15:45:23.006Z]   File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
[2022-07-09T15:45:23.006Z]   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
[2022-07-09T15:45:23.006Z]   File "<frozen importlib._bootstrap_external>", line 850, in exec_module
[2022-07-09T15:45:23.006Z]   File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
[2022-07-09T15:45:23.006Z]   File "/Users/bartoszlachowicz/Desktop/CodeProjects/customers-slack-app/__app__/overview/__init__.py", line 9, in <module>
[2022-07-09T15:45:23.006Z]     import pyodbc
[2022-07-09T15:45:23.007Z] .

After reproducing from our end, We could able to see that the ODBC drivers were missing. After installing the ODBC Drivers we could able to make this work.

REFERENCES: Install the Microsoft ODBC driver for SQL Server (macOS)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM