如何使用Anaconda安装OpenCV 2版本?新手技术咨询
Hey there! I totally get how frustrating it is when most online tutorials fixate on newer versions like OpenCV 3+, but you specifically need OpenCV 2. Let’s break down the exact steps to get it set up without any hassle.
Step 1: Create a New Anaconda Environment (Highly Recommended)
It’s always smart to use a dedicated environment for older library versions—this avoids messing up your base Anaconda setup with conflicting dependencies. Here’s how to make one:
- Open Anaconda Prompt (Windows) or your terminal (Mac/Linux)
- Run this command to create an environment named
opencv2_envwith a Python version compatible with OpenCV 2 (Python 3.6 or 3.7 works best—OpenCV 2 doesn’t support Python 3.8+):conda create -n opencv2_env python=3.6 - When prompted, type
yand hit Enter to proceed.
Step 2: Activate the Environment
Once the environment is created, activate it using:
conda activate opencv2_env
You’ll see the environment name (opencv2_env) show up at the start of your command line prompt when it’s active.
Step 3: Install OpenCV 2
There are two reliable ways to install OpenCV 2 in your environment:
Option 1: Using Conda-Forge (Preferred)
The default Anaconda channel might not host older OpenCV versions, so we’ll use the conda-forge channel which maintains more historical releases:
- First, you can check all available OpenCV 2 versions on conda-forge with this command:
conda search -c conda-forge opencv | grep "2\." - Install the latest available OpenCV 2 version (e.g., 2.4.13.7) with:
conda install -c conda-forge opencv=2.4.13.7 - Again, type
ywhen prompted to confirm the installation.
Option 2: Using Pip (If Conda Installation Fails)
If the conda method hits snags, you can use pip instead. Run this command to install the matching OpenCV 2 version:
pip install opencv-python==2.4.13.7
Step 4: Verify the Installation
To make sure everything worked correctly:
- Open Python in your active environment by running
pythonin the terminal - Type these lines one by one:
import cv2 print(cv2.__version__) - If you see a version number starting with
2.(like2.4.13.7), congratulations—you’ve successfully installed OpenCV 2!
Quick Notes
- Python Version Check: Double-check you’re using Python 3.6/3.7 in your environment. Confirm with
python --version—newer Python versions will cause installation errors. - Network Issues: If downloads are slow, you can configure a domestic conda mirror source to speed things up (just search for "conda domestic mirror" and follow region-specific setup steps).
内容的提问来源于stack exchange,提问作者s.bhardwaj

