# PythonCall.pyimport opencv on Windows

**URL:** <https://discourse.julialang.org/t/pythoncall-pyimport-opencv-on-windows/124146>\
**Category:** General Usage\
**Tags:** windows, pythoncall, opencv\
**Created:** [December 24, 2024, 8:29am UTC](https://discourse.julialang.org/t/pythoncall-pyimport-opencv-on-windows/124146 "2024-12-24T08:29:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [December 24, 2024, 8:29am UTC](https://discourse.julialang.org/t/pythoncall-pyimport-opencv-on-windows/124146/1 "2024-12-24T08:29:10Z")

</div>

I’m trying to `PythonCall.pyimport` `opencv` on Windows:

```julia
const cv2 = PythonCall.pynew()
function __init__ ()
    PythonCall.pycopy!(cv2, pyimport("cv2"))
end

```

but I run into

```md
Python: ImportError: DLL load failed while importing cv2: The specified procedure could not be found.

```

[Here](https://github.com/yakir12/CameraCalibrations.jl/actions/runs/12467510480/job/34796933749#step:6:1117) are the CI reports (you can see that it works perfectly fine in all the other OS).

Has anyone seen something like this and knows how to set up Python dependencies for Windows-CI, specifically for `opencv`?

> Why not use [GitHub - JuliaImages/OpenCV.jl: Use OpenCV in Julia!! 🚀](https://github.com/JuliaImages/OpenCV.jl)?

Excellent question! I did, and it was marvelous (i.e. easier and faster than using `opencv` via `python`), but it randomly failed tests (see [here](https://github.com/yakir12/CameraCalibrations.jl/actions/runs/12444670068) for CI reports). I’m pretty sure the issue is in `OpenCV.calibrateCamera`. The problem was that `OpenCV.calibrateCamera` was returning nonsensical values (1e-300 etc).  
I’m in the process of writing tests for `OpenCV.jl` in hopes of solving this issue.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [December 26, 2024, 12:07am UTC](https://discourse.julialang.org/t/pythoncall-pyimport-opencv-on-windows/124146/2 "2024-12-26T00:07:48Z")

</div>

My guess is that you have run into DLL Hell.

I would try to figure out the dependency situation using a Windows dependency walker such as the following:

> **[GitHub - lucasg/Dependencies: A rewrite of the old legacy software "depends.exe"...](https://github.com/lucasg/Dependencies)**
>
> A rewrite of the old legacy software "depends.exe" in C# for Windows devs to troubleshoot dll load dependencies issues.

Alternatively, try to load the opencv\_JLL firs.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [December 26, 2024, 12:48pm UTC](https://discourse.julialang.org/t/pythoncall-pyimport-opencv-on-windows/124146/3 "2024-12-26T12:48:51Z")

</div>

Okay…

1. Thank you very much, you solved that issue ❤
2. I still don’t:
  - know if what you meant was to add `OpenCV_jll` as a dependency to my package and `using OpenCV_jll`.
  - understand why in the world _that_ would work (but it does!)
  - accept that while `opencv` via `PythonCall` together with `OpenCV_jll` works, each alone doesn’t…

In any case, I hope we can solve the issues in `OpenCV_jll` and just ignore this oddity. For the curious, here’s my PR regarding that: [add tests for chessboard corner detection by yakir12 · Pull Request #52 · JuliaImages/OpenCV.jl · GitHub](https://github.com/JuliaImages/OpenCV.jl/pull/52)

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [December 26, 2024, 3:25pm UTC](https://discourse.julialang.org/t/pythoncall-pyimport-opencv-on-windows/124146/4 "2024-12-26T15:25:56Z")

</div>

> [@yakir12](#):
>
> I still don’t:
> 
> - know if what you meant was to add `OpenCV_jll` as a dependency to my package and `using OpenCV_jll`.
> - understand why in the world _that_ would work (but it does!)

Why it would work is that Python’s opencv will load some version of libopencv, which probably depends on some library that is incompatible with a library already loaded by Julia. By loading OpenCV\_jll first (which will also load libopencv) you essentially force a compatible set of libraries to be loaded first and then the Python opencv package uses those instead.

A fix which avoids depending on OpenCV\_jll is to find which library is clashing and ensure a compatible version is used instead - but it is particularly annoying to do this on Windows (“DLL hell”).

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [December 26, 2024, 3:42pm UTC](https://discourse.julialang.org/t/pythoncall-pyimport-opencv-on-windows/124146/5 "2024-12-26T15:42:22Z")

</div>

Thank you, that explains it very well.

I’ll keep hacking at improving `OpenCV.jl` instead, that seems like the future anyways.
