Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
548 views
in Technique[技术] by (71.8m points)

opencv - Conda does not install latest available version

On Ubuntu in a Conda environment with Python 3.7.3, when I run

conda install -c conda-forge opencv 

I get OpenCV 3.4.2 (checked with import cv2 and then cv2._version__) even though https://anaconda.org/conda-forge/opencv indicates version 4.11. Why?

Note that I didn't have OpenCV installed previously (I ran conda uninstall opencv and it got completely removed)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

tl;dr You likely have previously installed dependencies that need updating. If you require a specific version, say 4.1, then express this to Conda:

conda install -c conda-forge opencv=4.1

Explanation

How Conda Interprets Specifications

A literal translation of the command

conda install -c conda-forge opencv

would go something like

With the conda-forge channel included, ensure that some version of the package opencv is installed in the currently active environment.

The logic here implies that any version it can install would be a valid solution. It also doesn't tell it that it must come from Conda Forge, only that that channel should be included.1

Two-Stage Solve Strategy

Starting with v4.7, Conda uses a two-stage dependency solving strategy. The two stages are

  1. Solve with an implicit --freeze-installed|--no-update-deps flag. This attempts to find the newest version of the requested package that has no conflicts with installed packages. That is, it considers any installation of the package, no matter the version, to be a satisfactory solution. If it works, then it's done. Otherwise, move on to...
  2. An unrestricted solve (what used to be default in Conda < 4.7). This frees up dependencies to be updated and will often result in the latest versions being installed unless there are previous explicit specifications on those packages.2

This strategy aims to provide a faster solve and install experience, by avoiding having to change anything in your environment. It also helps keep the environment stable by avoiding unnecessary version changes.

Specific Failure in Question

What happened in OP's case? One of the dependencies requirements of OpenCV was likely newer in v4.1.1 than what was already installed, but that dependency's version was compatible with installing OpenCV 3.4.2. Hence, the only thing that would change was adding opencv plus missing dependencies. Technically, this is a valid solution since one only asked for some version of opencv to be installed.


Getting the Latest Version

Option: Specifying the Version

If you know you want a specific version then you can always specify it

conda install -c conda-forge opencv=4.1.1

and since Conda can't install this without updating something in your env, the first round of solve will fail, and the full solve will get it for you.

Option: Skip the Freeze

Of course, you may not always know what the latest version number is and don't want to have to look this up on Anaconda Cloud every time. Fortunately, there is the --update-deps flag that essentially skips over the first solve stage and goes straight to the full solve. This will install the latest version for your system, as well as update any of the dependencies.

conda install --update-deps -c conda-forge opencv

Important Note: The --update-deps flag has a side-effect of converting dependencies to explicit specifications. While this is an internal environment state (managed through <env>/conda-meta/history), it does have some behavioral consequences (bugs!):

  • the result of the conda env export --from-history command will subsequently include all packages, instead of just the ones the user explicitly requested in the past
  • conda remove will not be able to prune dependencies; e.g., if scipy was installed, it would pull in numpy; if only scipy depended on numpy and scipy was removed, normally numpy would also get removed. This wouldn't work after using the --update-deps flag.

[1]: The behavior here depends on the channel_priority configuration option. With the strict setting, conda-forge would be prioritized over other channels; with the flexible setting, it is simply added to the list and the latest compatible version from any channel is selected.

[2]: One can check the explicit specifications of an environment with conda env export --from-history.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...