question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot load `*.so` libraries in a directory specified by AppSetting(Environment variables) `LD_LIBRARY_PATH`.

See original GitHub issue

I want to load so libraries for python package opencv-python-headless.

Investigative information

  • Timestamp: 2019-10-06 03:48:02 (UTC)
  • InvocationId: 58ab63f9-096e-423a-be88-ef1870d98a79
  • Function App name: pythonfunc1
  • Function name(s) (as appropriate): HttpTrigger
  • Core Tools version: 2.7.1633

Repro steps

  1. Deploy function app package including some so files.
    The file structure is following.
$ tree .
.
├── host.json
├── HttpTrigger
│   ├── function.json
│   ├── __init__.py
│   └── sample.dat
├── local.settings.json
├── proxies.json
├── requirements.txt
└── tmp
    ├── libglib-2.0.so.0
    └── libgthread-2.0.so.0
  1. Set App Setting LD_LIBRARY_PATH to /home/site/wwwroot/tmp in order to load above so files. image

  2. Execute the function.

Expected behavior

import cv2 in __init__.py will work without any error/exception

Actual behavior

Following exception occurred, of cause http response code is 500.

Result: Failure
Exception: ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory

image

I confirmed that /home/site/wwwroot/tmp was set to enviroment variable LD_LIBRARY_PATH by using os.getenv('LD_LIBRARY_PATH') in my function program. I’m not sure why *.so libraries in a path specified by LD_LIBRARY_PATH cannot be loaded even though the path can be set appropriately in the environment variable LD_LIBRARY_PATH.

Known workarounds

The function with following code instead of import cv2

import ctypes
import importlib

exlibpath = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + '/tmp/'
ctypes.CDLL(exlibpath + 'libglib-2.0.so.0')
ctypes.CDLL(exlibpath + 'libgthread-2.0.so.0')

cv2 = importlib.import_module('cv2')

Related information

  • Links to source
  • Contents of the requirements.txt file
  • Bindings used
Source Function code:
# __init__.py

import logging
import os
import urllib.request as rq
import re
import azure.functions as func
import mimetypes

import cv2

# work around code
# import ctypes
# import importlib

# exlibpath = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + '/tmp/'
# ctypes.CDLL(exlibpath + 'libglib-2.0.so.0')
# ctypes.CDLL(exlibpath + 'libgthread-2.0.so.0')

# cv2 = importlib.import_module('cv2')

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    image_url = req.params.get('image_url')
    if not image_url:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            image_url = req_body.get('image_url')

    if image_url:
        local = re.sub(r'^.*/([^/]+)$', r'\1', image_url)
        rq.urlretrieve(image_url, '/tmp/' + local)
        im = cv2.imread('/tmp/' + local)
        gry = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        cv2.imwrite('/tmp/gray_' + local, gry)
        with open('/tmp/gray_' + local, 'rb') as f:
            mimetype = mimetypes.guess_type('/tmp/gray_' + local)
            return func.HttpResponse(f.read(), mimetype=mimetype[0])
        # return func.HttpResponse(f"Hello {image_url}!")
    else:
        return func.HttpResponse(
             "Please pass a image_url on the query string or in the request body",
             status_code=400
        )

requirements.txt:

opencv-python-headless

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
mlazowikcommented, Oct 31, 2019

Python uses ld to find libraries in LD_LIBRARY_PATH, but ld is not present in the runtime. https://hg.python.org/cpython/rev/385181e809bc#l3.10

0reactions
anirudhgargcommented, Apr 29, 2020

@horihiro @mlazowik we have added the binutils to the python images and also added the .so libraries needed for OpenCV. Not sure at this point how we can fix this issue of loading any *.so so closing this out.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Conda set LD_LIBRARY_PATH for env only - Stack Overflow
You can set environment variables when an environment is activated by editing the activate.d/env_vars.sh script.
Read more >
Setting the library path environment variable - IBM
In this case, the libraries from directory /opt/IBM/InformationServer/Server/branded_odbc/lib are loaded when the job runs because this directory appears before ...
Read more >
Manage Shared Libraries with Environment Variables
If your package recipe (A) is generating shared libraries you can declare the needed environment variables pointing to the package directory.
Read more >
How to create appSettings and Environment Variables in project
As part of this agenda we are going to implement an appSettings and Environment variables in your project in C# .NET Core 3.1...
Read more >
Error finding shared libraries when running executable
so libraries is not found: error while loading shared libraries: example.so: cannot open shared object file: No such file or directory I have ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found