Results 1 to 5 of 5

Thread: Python ImportError for custom C++ module

  1. #1
    Join Date
    Apr 2008
    Beans
    20
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Python ImportError for custom C++ module

    Hi,

    I've been developing a Python module in C++ using OpenCV 2.3 through 2.4.2, on Ubuntu 11.04. OpenCV was built from source. I'm not using the version of OpenCV from the Ubuntu repositories.

    The module compiles with no issues and is loaded in Python properly.

    However, when I compile this module on Ubuntu 11.10 or 12.04, I get an ImportError when trying to load it in Python.

    This is how I compile the module:

    Code:
    g++ -fPIC -shared `pkg-config --cflags --libs python` `pkg-config --cflags --libs opencv` -I/usr/local/include/opencv2/legacy -o mymodule.so mymodule.cpp
    This is the output of "pkg-config --cflags --libs opencv"

    Code:
    -I/usr/local/include/opencv -I/usr/local/include  /usr/local/lib/libopencv_calib3d.so /usr/local/lib/libopencv_contrib.so /usr/local/lib/libopencv_core.so /usr/local/lib/libopencv_features2d.so /usr/local/lib/libopencv_flann.so /usr/local/lib/libopencv_gpu.so /usr/local/lib/libopencv_highgui.so /usr/local/lib/libopencv_imgproc.so /usr/local/lib/libopencv_legacy.so /usr/local/lib/libopencv_ml.so /usr/local/lib/libopencv_nonfree.so /usr/local/lib/libopencv_objdetect.so /usr/local/lib/libopencv_photo.so /usr/local/lib/libopencv_stitching.so /usr/local/lib/libopencv_ts.so /usr/local/lib/libopencv_video.so /usr/local/lib/libopencv_videostab.so
    The error I get is:

    Code:
    ImportError: /path/to/service/mymodule.so: undefined symbol: _ZN5CvSVMD1Ev
    My understanding is that the ImportError generally means that the given symbol can't be found in any of the linked libraries. But I know that this symbol is there in libopencv_ml.so because when I run this:

    Code:
    $ nm -g  /usr/local/lib/libopencv_ml.so | grep _ZN5CvSVMD1Ev
    I get:

    Code:
    000000000002fd40 T _ZN5CvSVMD1Ev
    So can you give me any clue what I might be doing wrong? Has something changed between 11.04 and 11.10 in the manner in which shared libraries are loaded when running Python?
    Or is this a problem with OpenCV?

    Thanks,
    Rajesh

  2. #2
    Join Date
    Apr 2008
    Beans
    20
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: Python ImportError for custom C++ module

    Can anyone give me a clue about where to continue debugging?

  3. #3
    Join Date
    Jul 2012
    Beans
    54

    Re: Python ImportError for custom C++ module

    You did not run the "nm" command on your mymodule.so so I would assume the symbol really is missing.

    I have no idea how you can fix that. That is a really strange name for a symbol.

  4. #4
    Join Date
    May 2007
    Beans
    251

    Re: Python ImportError for custom C++ module

    Most likely, your libopencv_ml.so is not found in the LD_LIBRARY_PATH for your python process.
    As an experiment to confirm it, you could try the following:
    Code:
    $ LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib python
    And at the python prompt, try importing your module.

    If this solves the problem, check if /usr/local/lib is included in one of the files under /etc/ld.so.conf.d/ (or in /etc/ld.so.conf).
    The Unforgiven

  5. #5
    Join Date
    Apr 2008
    Beans
    20
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: Python ImportError for custom C++ module

    ld.so.conf.d did contain a reference to /usr/local/lib, so that wasn't the problem.

    The problem was apparently in the way I was compiling the module. Running 'ldd' on my module, I got only five linked libraries, and libopencv_ml.so wasn't one of them.

    Instead of

    Code:
    g++ -fPIC -shared `pkg-config --cflags --libs python` `pkg-config --cflags --libs opencv` -I/usr/local/include/opencv2/legacy -o mymodule.so mymodule.cpp
    I needed to change it to

    Code:
    g++ -fPIC -shared -o mymodule.so mymodule.cpp `pkg-config --cflags --libs python` `pkg-config --cflags --libs opencv` -I/usr/local/include/opencv2/legacy
    ldd then gave me 75 linked libraries, and the code now works.

    I still don't understand WHY it behaves this way, so if someone can throw some light on that, that would be helpful.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •