Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add OpenCV as precompiled library in Poetry?

I am trying to add OpenCV to my virtual environment with Poetry. OpenCV is already installed globally and can be used globally in python, but not in a virtual environment created by Poetry.

I added the library to poetry like this:

[tool.poetry.dependencies]
cv2 = { path = "/usr/local/lib/python3.8/site-packages/cv2/python-3.8/cv2.cpython-38-aarch64-linux-gnu.so" }

When trying to install it with poetry install I get the following error message:

ReadError

file could not be opened successfully

at /usr/local/lib/python3.8/tarfile.py:1608 in open
      1604│                 except (ReadError, CompressionError):
      1605│                     if fileobj is not None:
      1606│                         fileobj.seek(saved_pos)
      1607│                     continue
    → 1608│             raise ReadError("file could not be opened successfully")
      1609│
      1610│         elif ":" in mode:
      1611│             filemode, comptype = mode.split(":", 1)
      1612│             filemode = filemode or "r"

How can I add globally installed precompiled libraries to my poetry environment?

like image 932
Jan Gerber Avatar asked Oct 28 '25 08:10

Jan Gerber


1 Answers

Running poetry add opencv-python in your CLI while you're in the project directory should accomplish your end goal. To use cv2, include import cv2 in your Python code.

It seems that your question, however, pertains specifically to depending on a local installation using the path property described in Poetry's documentation for dependency specification. I believe your approach should work if you're pointing to a tarball or zip file, but that's not the case for the file you mention (pip needs other files besides the one you gave).

Why not try pointing to the tar/gzip file (e.g., 'opencv-python-4.5.5.64.tar.gz') available through their Downloads page on PyPI instead?

EDIT: If you're using a containerizer like Docker, or Jupyter notebooks or some other GUI that already has the ability to display images built-in, you may wish to use the lighter-weight implementation, opencv-python-headless.

like image 110
BLT Avatar answered Oct 30 '25 05:10

BLT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!