You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

求助:在Anaconda中安装opencv-python包的步骤及版本冲突解决

在Anaconda中安装opencv-python及解决版本冲突问题

Hey there, let's walk through how to get opencv-python set up properly in Anaconda, and fix that numpy version mismatch issue you ran into in PyCharm.

第一步:准备Anaconda虚拟环境(关键!避免版本混乱)

First off, never install packages directly in the base environment—it's a surefire way to get version conflicts. Let's create a clean environment for your computer vision work:

  • Open your Anaconda Prompt (or terminal on Linux/macOS)
  • Create a new environment (replace cv_env with your preferred name, and pick a Python version between 3.8-3.10—these are most compatible with opencv):
    conda create -n cv_env python=3.9
    
  • Activate the environment:
    conda activate cv_env
    

第二步:安装opencv-python及匹配的依赖

We'll use the conda-forge channel here because it has up-to-date, pre-built opencv packages that automatically handle dependency versions (like numpy):

  1. Add the conda-forge channel (if you haven't already) to prioritize compatible packages:
    conda config --add channels conda-forge
    conda config --set channel_priority strict
    
  2. Install opencv-python—this will pull in the exact numpy version it needs:
    conda install opencv-python
    
    If you prefer using pip instead, run this in your activated environment:
    pip install opencv-python
    
    Either way, the package manager will resolve numpy compatibility automatically.

第三步:修复PyCharm中的导入问题 & 版本冲突

The error you saw is almost certainly because PyCharm was using a different environment (with a mismatched numpy version) than the one where you installed opencv. Here's how to fix it:

  1. Open your project in PyCharm, go to File > Settings > Project: [Your Project Name] > Python Interpreter
  2. Click the gear icon in the top-right corner, select Add
  3. Choose Conda Environment > Existing environment, then browse to the Python executable of your cv_env environment:
    • Windows: C:\Users\[Your Username]\anaconda3\envs\cv_env\python.exe
    • Linux/macOS: ~/anaconda3/envs/cv_env/bin/python
  4. Click OK to apply the changes. Now PyCharm will use the environment with the compatible opencv and numpy versions.

如果你已经有一个混乱的环境:

If you tried installing opencv in an existing environment and have numpy conflicts, clean it up:

  • Activate the problematic environment: conda activate your_old_env
  • Uninstall both opencv and numpy:
    conda uninstall opencv-python numpy
    
  • Reinstall opencv using the steps above—this will pull in the correct numpy version automatically.

验证安装

To make sure everything works, open a Python console in PyCharm (or your activated Anaconda environment) and run:

import cv2
print(cv2.__version__)

If it prints the opencv version without errors, you're good to go!

内容的提问来源于stack exchange,提问作者Abhishek Jain

火山引擎 最新活动