python - Trouble importing mediapipe (Ubuntu 24.04.2 LTS) - Stack Overflow

I followed the official Google tutorial to install MediaPipe:MediaPipe Installation GuideThe installa

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:58] Hello World!
I0000 00:00:1741695160.633054   70976 hello_world: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:

  • Checking if mediapipe is installed using pip show mediapipe, but it says Package(s) not found.
  • Running my script outside VSCode (python my_script.py), but the error persists.
  • Uninstall and install mediapipe again.

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

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:58] Hello World!
I0000 00:00:1741695160.633054   70976 hello_world: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:

  • Checking if mediapipe is installed using pip show mediapipe, but it says Package(s) not found.
  • Running my script outside VSCode (python my_script.py), but the error persists.
  • Uninstall and install mediapipe again.

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

Share Improve this question edited Mar 11 at 18:55 Paolo 21.2k21 gold badges76 silver badges122 bronze badges asked Mar 11 at 12:29 Alexis Lasselin Alexis Lasselin 335 bronze badges 1
  • Editing "solved" into question titles is not allowed. The only way to mark a question solved is to click the checkbox next to an answer. Since that's already done, nothing else is needed. – Charles Duffy Commented Mar 11 at 16:50
Add a comment  | 

1 Answer 1

Reset to default 1

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

  • activating command palette (Cmd + Shift + p)

  • Search for Python: Select Interpreter

  • Select Enter interpreter Path => Find ...

  • Navigate to

    /path/to/your/mediapipe_install/.venv/bin/python
    
    

    Confirm selection.

  • check in VScode terminal:
    python -c "import sys; print(sys.executable)" - should give the python path.

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744795208a4594164.html

相关推荐

  • python - Trouble importing mediapipe (Ubuntu 24.04.2 LTS) - Stack Overflow

    I followed the official Google tutorial to install MediaPipe:MediaPipe Installation GuideThe installa

    2天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信