Opencv error cv2

I am trying to deploy an application that use opencv but i am encountering this error

i already tried to modify my nix file

see nix file

{ pkgs }: {
  deps = [
    pkgs.run
    pkgs.libGL
  ];
}

Hey, @dragonchild870!

Can you please provide a link to the repl? This way it is easier for staff and members of the community to help you!

Also see this guide on how to share your code:

Hey, @dragonchild870! Welcome to the forums!

Running OpenCV requires some extra configuration. Because of this, the staff have released a template that has everything configured and OpenCV installed here: https://replit.com/@scottatreplit/Python-OpenCV?v=1
Hope this helps!

2 Likes

can you teach me how to install opencv properly only? i already check the template but there are a lot of things that i dont need there i just need the opencv only

this is my repl https://replit.com/@admin324/Python-OpenCV

Hi @dragonchild870 !
What is wrong with the template I mentioned earlier? You could delete the code in main.py if you don’t want that, and replace it with your own code (but don’t change replit.nix or .replit).

1 Like

well i dont really have problem with it i just want to adapt it with my own setup, i dont use venv and python is 3.10 but the template is using 3.9 and older so if i can only install opencv properly thats enough for me, i want to gain understanding how to properly troubleshoot it

Try running

curl -s https://firepup650.repl.co/raw/minifier | bash -s

in the Shell of the repl. Make sure you take note of the Nix packages before entring the command in case they disappear.
@ShaneAtReplit could the template be updated to the latest version of Python and the Python template (the venv folder etc).

@dragonchild870 The command allows the repl to take up less space, as well as updating the structure to the current Python repl. Made by a Jr. Mod on this forum. I have used it myself many times, works like a gem. It uninstalls packages though, so install cv2 afterwards maybe?

1 Like

what happen in my situation was i installed opencv then after that its looking for libGL.so.1 like the screenshot i shared so what should i do next to fix that like is there something i need to change in nix file like this ?{ pkgs }: {
deps = [
pkgs.run
pkgs.libGL
];
} as much as possible i only need the openCV like i want to learn how to properly fix it without using the template i dont have any problem with the template i just dont want to rely on the template

Maybe try using the Nix modules used in the template?

IIRC, you have to explicitly add some extra configuration file somewhere, which is why Replit staff have just provided that template instead.

1 Like

The configuration for this might be in replit.nix, where it makes a library path that includes libGL.

{ pkgs }: {
  deps = [
    pkgs.python38Full
  ];
  env = {
    PYTHON_LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath [
      # Neded for pandas / numpy
      pkgs.stdenv.cc.cc.lib
      pkgs.zlib
      # Needed for pygame
      pkgs.glib
      # Needed for matplotlib
      pkgs.xorg.libX11
      # Needed for opencv-python
      pkgs.libGL  # <- new in OpenCV template
    ];
    PYTHONBIN = "${pkgs.python38Full}/bin/python3.8";
    LANG = "en_US.UTF-8";
  };
}

The current official python template requires no nix dependencies and does not set an env variable here (so it might be hard to configure a new repl). The older python templates use prybar instead of python directly.

i have a question so i used the template and resolve everything but last problem i have is how can run it with streamlit? this is the file whenever i press the run button it just say run it with command ‘streamlit run main.py’ which i already setup in the file below its working fine but how about you need to add new package? how can you configure it? like for example i want to use streamlit with it how can i configure it?
its also throwing error /usr/bin/env: ‘./python3’: No such file or directory when i try to run streamlit run main.py in shell

disableInstallBeforeRun = true

# The command that runs the program.
run = "streamlit run main.py --server.port 8080"

# The primary language of the repl. There can be others, though!
language = "python3"
# The main file, which will be shown by default in the editor.
entrypoint = "main.py"
# A list of globs that specify which files and directories should
# be hidden in the workspace.
hidden = ["venv", ".config", "**/__pycache__", "**/.mypy_cache", "**/*.pyc"]

# Specifies which nix channel to use when building the environment.
[nix]
channel = "stable-21_11"

# Per-language configuration: python3
[languages.python3]
# Treats all files that end with `.py` as Python.
pattern = "**/*.py"
# Tells the workspace editor to syntax-highlight these files as
# Python.
syntax = "python"

  # The command needed to start the Language Server Protocol. For
  # linting and formatting.
  [languages.python3.languageServer]
  start = ["pyls"]

# The command to start the interpreter.
[interpreter]
  [interpreter.command]
  args = [
    "stderred",
    "--",
    "prybar-python3",
    "-q",
    "--ps1",
    "\u0001\u001b[33m\u0002\u0001\u001b[00m\u0002 ",
    "-i",
  ]
  env = { LD_LIBRARY_PATH = "$PYTHON_LD_LIBRARY_PATH" }

# The environment variables needed to correctly start Python and use the
# package proxy.
[env]
VIRTUAL_ENV = "/home/runner/${REPL_SLUG}/venv"
PATH = "${VIRTUAL_ENV}/bin"
PYTHONPATH="${VIRTUAL_ENV}/lib/python3.8/site-packages"
REPLIT_POETRY_PYPI_REPOSITORY="https://package-proxy.replit.com/pypi/"
MPLBACKEND="TkAgg"

# Enable unit tests. This is only supported for a few languages.
[unitTest]
language = "python3"

# Add a debugger!
[debugger]
support = true

  # How to start the debugger.
  [debugger.interactive]
  transport = "localhost:0"
  startCommand = ["dap-python", "main.py"]

    # How to communicate with the debugger.
    [debugger.interactive.integratedAdapter]
    dapTcpAddress = "localhost:0"

    # How to tell the debugger to start a debugging session.
    [debugger.interactive.initializeMessage]
    command = "initialize"
    type = "request"

      [debugger.interactive.initializeMessage.arguments]
      adapterID = "debugpy"
      clientID = "replit"
      clientName = "replit.com"
      columnsStartAt1 = true
      linesStartAt1 = true
      locale = "en-us"
      pathFormat = "path"
      supportsInvalidatedEvent = true
      supportsProgressReporting = true
      supportsRunInTerminalRequest = true
      supportsVariablePaging = true
      supportsVariableType = true

    # How to tell the debugger to start the debuggee application.
    [debugger.interactive.launchMessage]
    command = "attach"
    type = "request"

      [debugger.interactive.launchMessage.arguments]
      logging = {}

# Configures the packager.
[packager]
# Search packages in PyPI.
language = "python3"
# Never attempt to install `unit_tests`. If there are packages that are being
# guessed wrongly, add them here.
ignoredPackages = ["unit_tests"]

  [packager.features]
  enabledForHosting = false
  # Enable searching packages from the sidebar.
  packageSearch = true
  # Enable guessing what packages are needed from the code.
  guessImports = true

Run the main.py file normally…? Not working?