How to add OpenCV as precompiled library in Poetry?

Viewed 1971

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 libararies to my poetry environment?

2 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.

remove the line cv2 in the pyproject.toml. in poetry we use 'poetry add packagename' command to add a package in a virtual environement for example, poetry add django-restframework. make sure that your terminal is open in the same folder that the pyproject.toml file.

Related