pythonubuntumediapipe

Trouble importing mediapipe (Ubuntu 24.04.2 LTS)


I followed the official Google tutorial to install MediaPipe:
MediaPipe Installation Guide

The installation seems successful, and I can run the test script without issues, getting the following output:

I0000 00:00:1741695160.632977   70976 hello_world.cc:58] Hello World!
I0000 00:00:1741695160.633054   70976 hello_world.cc:58] Hello World!
...

However, when I try to import MediaPipe in my own Python script, I get the following error in VSCode (Pylance):

Import "mediapipe" could not be resolved Pylance(reportMissingImports)

Here’s an excerpt from my script:

import cv2 
import mediapipe as mp 

mp_holistic = mp.solutions.holistic  
mp_drawing = mp.solutions.drawing_utils  

I am using Ubuntu 24.04.2 LTS and VSCode with Pylance.

I already tried the following:

Has anyone encountered this issue before? How can I resolve it?


Solution

  • I am re-posting this answer - because it was deleted.

    It is true - I posted the same answer somwhere else - but it was an older question. And IF deleted - I ask to delete the other answer and not this. Because I wrote this answer for this question originally and not the other older question.

    And I don't see a problem in posting the same answer to different questions - if the answers are absolutely correct and legit.

    Plus the other question was specifically for MacOS while this is for Linux / Ubuntu.

    It just happens that you can do the same steps - that is why I didn't change anything in the answer. They could differ. But in this case they don't!

    ---

    I guess it has to do with that the package could not be installed properly.

    Since recently, I am using uv package manager, which is a one-tool-for-all-other-python-tools solution, makes installation much more reliable and faster (100x).

    1. install uv in your ubuntu/macos

    # with curl
    curl -LsSf https://astral.sh/uv/install.sh | sh
    
    # with wget
    wget -qO- https://astral.sh/uv/install.sh | sh
    

    2. create a new virtual environment with uv

    uv init ~/mediapipe_install # change path to your folder path
    # cd to the environment
    cd ~/mediapipe_install
    

    3. install python, opencv and mediapipe into this virtual environment

    # install your own python version 
    uv python install 3.12
    # activate this freshly installed python
    uv venv --python 3.12
    
    # verify correct installation of python
    uv run python -c "import sys; print(sys.executable)"
    # the path should be sth like
    # /path/to/your/mediapipe_install/.venv/bin/python
    
    # install opencv in the uv virtual environment
    uv pip install opencv-python opencv-contrib-python
    # install then mediapipe
    uv pip install mediapipe
    
    # verify openCV installation
    uv run python -c "import cv2; print(cv2.__version__)"
    # verify mediapipe installation
    uv run python -c "import mediapipe as mp; print(mp.__version__)"
    

    4. connect VSCode with this uv environment python by

    Voila! Now, your import mediapipe as mp will work as well as your import cv2 !