NVIDIA / VideoProcessingFramework

Set of Python bindings to C++ libraries which provides full HW acceleration for video decoding, encoding and GPU-accelerated color space and pixel format conversions
Apache License 2.0
1.32k stars 233 forks source link

Cannot import PyNvCodec after pip installation. #429

Closed quqixun closed 1 year ago

quqixun commented 1 year ago

Describe the bug Cannot import PyNvCodec after pip installation.

To Reproduce Install VPF as shown in README, then import.

apt install -y \
    libavfilter-dev \
    libavformat-dev \
    libavcodec-dev \
    libswresample-dev \
    libavutil-dev\
    wget \
    cmake \
    build-essential \
    git \
    libnvidia-encode-525 \
    libnvidia-decode-525

pip3 install git+https://github.com/NVIDIA/VideoProcessingFramework
import PyNvCodec

Expected behavior After installation and import, the following error occured.

Traceback (most recent call last):
  File "/home/user/miniconda3/envs/vpf/lib/python3.9/site-packages/PyNvCodec/__init__.py", line 17, in <module>
    from ._PyNvCodec import *  # noqa
ImportError: /lib/x86_64-linux-gnu/libgobject-2.0.so.0: undefined symbol: ffi_type_uint32, version LIBFFI_BASE_7.0

Desktop (please complete the following information):

theHamsta commented 1 year ago

Can you check whether you did the pip install and the import in the same environment? Either both inside the vpf environment or both outside and that the pip you were using is the pip of the anaconda env? To be sure you could run python -mpip install ..

Does the installation work outside of your anaconda environment? Anaconda changes several environment variables that affect library discovery. It should have access to the same libraries as during pip install. The only dependency of vpf is numpy. So it shouldn't mess up your global environment.

Since it's printing libgobject.so, could you run ldd on the mentioned library. Ffmpeg could maybe use libgobject when build with certain extensions. I'm not aware that python would use glib. You can also run ldd on the vpf python extension itself.

You're using Ubuntu 20.04. How did you install ffmpeg? The system ffmpeg is too old on 20.04 and gets rejected from the pip install script. It might be also useful to uninstall vpf using pip and then provide the full log of pip install . --verbose

theHamsta commented 1 year ago

@quqixun could you solve your problem?

quqixun commented 1 year ago

It is still not work.

Here is what I have done:

mkdir -p Git
cd Git

git clone https://github.com/FFmpeg/FFmpeg.git
cd FFmpeg

mkdir -p $(pwd)/build_x64_release_shared 
./configure \
 --prefix=$(pwd)/build_x64_release_shared \
 --disable-static \
 --disable-stripping \
 --disable-doc \
 --enable-shared

make -j -s && make install
export LD_LIBRARY_PATH=~/Git/FFmpeg/build_x64_release_shared/lib:$LD_LIBRARY_PATH
export PATH=~/Git/FFmpeg/build_x64_release_shared/bin:$PATH
cd ..

conda create --name vpf python=3.9
conda activate vpf

git clone https://github.com/NVIDIA/VideoProcessingFramework.git
cd VideoProcessingFramework
pip install . --verbose
# full logs see vpf-pip-install-verbose.txt

ldd ~/miniconda3/envs/vpf/lib/python3.9/site-packages/PyNvCodec/_PyNvCodec.cpython-39-x86_64-linux-gnu.so
# full outpus see ldd-_PyNvCodec.cpython-39-x86_64-linux-gnu.so.txt

vpf-pip-install-verbose.txt and ldd-_PyNvCodec.cpython-39-x86_64-linux-gnu.so.txt

Then import PyNvCodec in vpf environment, and got the same errors:

Traceback (most recent call last):
  File "/home/quqixun/miniconda3/envs/vpf/lib/python3.9/site-packages/PyNvCodec/__init__.py", line 17, in <module>
    from ._PyNvCodec import *  # noqa
ImportError: /lib/x86_64-linux-gnu/libgobject-2.0.so.0: undefined symbol: ffi_type_uint32, version LIBFFI_BASE_7.0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/quqixun/miniconda3/envs/vpf/lib/python3.9/site-packages/PyNvCodec/__init__.py", line 21, in <module>
    raise RuntimeError("Failed to import native module _PyNvCodec! "
RuntimeError: Failed to import native module _PyNvCodec! Please check whether "/home/quqixun/miniconda3/envs/vpf/lib/python3.9/site-packages/PyNvCodec/_PyNvCodec.cpython-39-x86_64-linux-gnu.so" exists and can find all library dependencies (CUDA, ffmpeg).
On Unix systems, you can use `ldd` on the file to see whether it can find all dependencies.
On Windows, you can use "dumpbin /dependents" in a Visual Studio command prompt or
https://github.com/lucasg/Dependencies/releases.
quqixun commented 1 year ago

@theHamsta Did you reproduce the error ? In my system, /lib/x86_64-linux-gnu/libgobject-2.0.so.0 is a link to /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.6400.6 .

theHamsta commented 1 year ago

What I was suggesting was to try to compile and import without the conda environment. I assume that your Anaconda environment provides a different libffi version than you system. ffmpeg was probably configured to use your systems' libgobject which would use your systems's libffi.

I have very bad experience with Anaconda setting environement variables or compiling packages with Anaconda. Even when you're in this base environment, Anaconda has already manipulated a lot of library paths which cause a lot of problems. Try to install and compile everything without Anaconda changing anything in your .bashrc/.profile. If this works and you still want to use a virual environment, then I would also recommend to run ffmpeg's configure inside the Python environment.

You can also run ldd on /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.6400.6 to see whether the loaded deps are different inside and outside your Anaconda environment.

Did you run ldd outside the Python environment? It seems to find your systems libffi.so.7 => /lib/x86_64-linux-gnu/libffi.so.7 which should provide the symbol mentioned in the error message. What does it return inside the Python environment?.

Can you try to run

/home/quqixun/miniconda3/bin/python3.9 -c "import PyNvCodec"

outside of any Python environment? (path to python executable may be different)

quqixun commented 1 year ago

@theHamsta Thanks for the suggestion. I just update my os from ubuntu 20.04 to 22.04 (keep conda environment), then everything is fine. It looks good that compile VPF from source and run import in the save conda environment under ubuntu 22.04. All tests pass.

theHamsta commented 1 year ago

@theHamsta Thanks for the suggestion. I just update my os from ubuntu 20.04 to 22.04 (keep conda environment), then everything is fine. It looks good that compile VPF from source and run import in the save conda environment under ubuntu 22.04. All tests pass.

I assume that the system ffmpeg and the self-compiled one got mixed up in some way. Was it using pkg-config? We could apply the trick we use for Windows to automatically copy dependencies of the Python extension next to the Python extension so that you don't need to make sure that they are also in LD_LIBRARY_PATH during runtime.

quqixun commented 1 year ago

I assume that the system ffmpeg and the self-compiled one got mixed up in some way.

It maybe not the problem with system ffmpeg, because I cleaned the system ffmpeg before compilling ffmpeg from source.