Search code examples
pythonpippython-poetry

Why is cv2 not available although it should have been installed with `poetry add opencv-python`?


Abstract of Error

The following command fails on jupyter notebook.

import cv2
cv2.__version__

The error message is as follows.

ModuleNotFoundError: No module named 'cv2'.

My Attempt

poetry failed

My project uses poetry for package management and I first ran poetry add opencv-python.

The installation was successful and the version number was displayed by running poetry show opencv-python on the IDE terminal. However, I still got the same error on jupyter notebook.

pip success

Next, run pip install opencv-python on the IDE terminal, and the error disappears and I can now run it.

My Question

  • Why does pip work and poetry does not?

  • How can I use poetry alone to manage my project without using pip?

My Environment

  • IDE: VSCode 1.81.1

  • using DevContainer

    • image: jupyter/datascience-notebook:python-3.11.4
    • feature: ghcr.io/devcontainers-contrib/features/poetry
  • run jupyter notebook on VSCode. The kernel is python 3.11.4

  • put the poetry.toml file in the root of the project and added [virtualenvs] create = false to it.

  • Here is the result of poetry env info:

Virtualenv
Python:         3.11.4
Implementation: CPython
Path:           NA
Executable:     NA

System
Platform:   linux
OS:         posix
Python:     3.11.4
Path:       /usr/local/py-utils/venvs/poetry
Executable: /usr/local/py-utils/venvs/poetry/bin/python3.11
  • the output of poetry env list is nothing.

  • the output of poetry config --list | grep virtualenvs is:

virtualenvs.create = false
virtualenvs.in-project = null
virtualenvs.options.always-copy = false
virtualenvs.options.no-pip = false
virtualenvs.options.no-setuptools = false
virtualenvs.options.system-site-packages = false
virtualenvs.path = "{cache-dir}/virtualenvs"  # /home/jovyan/.cache/pypoetry/virtualenvs
virtualenvs.prefer-active-python = false
virtualenvs.prompt = "{project_name}-py{python_version}"
  • My pyproject.toml is:
...

[tool.poetry.dependencies]
python = "^3.11"
ipykernel = "^6.25.1"
opencv-python = "^4.8.0.76"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
  • The output of print(sys.executable) in notebook is /opt/conda/bin/python

  • The output of poetry run python3 -c 'import sys; print(sys.executable)' in IDE terminal is /usr/local/py-utils/venvs/poetry/bin/python3.


Solution

  • I don't know the exact details, but I suspect that there is a problem with the jupyter/datascience-notebook image that was used to create DevContainer.

    I was able to work around the problem by creating my own Docker image based on the template for poetry.

    https://github.com/Seasawher/poetry-devcontainer