Last modified: Oct 17, 2024 By Alexander Williams

How to Fix ModuleNotFoundError: No module named 'mmcv' in Python

When working with computer vision projects, particularly those related to OpenMMLab, you might encounter the error ModuleNotFoundError: No module named 'mmcv'. This error occurs when the MMCV package, which is a fundamental computer vision library, is not properly installed.

For more information about solving similar errors, check out our article on How To Solve ModuleNotFoundError: No module named in Python.

Understanding the Error

This error appears when you try to import MMCV without having it installed:


import mmcv

# This will raise the error if MMCV is not installed
cfg = mmcv.Config.fromfile('config.py')


Traceback (most recent call last):
  File "your_script.py", line 1, in 
    import mmcv
ModuleNotFoundError: No module named 'mmcv'

How to Fix the Error

Solution 1: Install MMCV using pip

The basic installation method using pip:


pip install mmcv-full

Solution 2: Install MMCV with CUDA support

For GPU acceleration, install MMCV with specific CUDA version (replace {cu_version} with your CUDA version):


pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu110/torch1.7.0/index.html

Solution 3: Install from source

For the latest development version:


git clone https://github.com/open-mmlab/mmcv.git
cd mmcv
pip install -e .

Prerequisites

Before installing MMCV, ensure you have:

  • Python 3.6 or higher
  • PyTorch installed (required for MMCV-full)
  • CUDA toolkit (if using GPU support)

Common Issues and Solutions

Version Compatibility

Make sure your PyTorch and CUDA versions are compatible with MMCV:


import torch
print(torch.__version__)  # Check PyTorch version
print(torch.version.cuda)  # Check CUDA version

Common Problems

  • Build Errors: Try installing the pre-built wheel instead of building from source
  • CUDA Mismatch: Ensure CUDA versions match between PyTorch and MMCV
  • Dependencies: Install required dependencies first:
    
    pip install numpy
    pip install pytorch
    
    

Verifying the Installation

After installation, verify MMCV is working correctly:


import mmcv
print(mmcv.__version__)

# Test basic functionality
cfg = mmcv.Config(dict(a=1, b=dict(b1=[0, 1])))
print(cfg)

Basic Usage Example

Here's a simple example using MMCV after installation:


import mmcv

# Read an image
img = mmcv.imread('image.jpg')

# Resize image
img_resized = mmcv.imresize(img, (224, 224))

# Save the processed image
mmcv.imwrite(img_resized, 'resized_image.jpg')

Conclusion

The ModuleNotFoundError: No module named 'mmcv' can be resolved by properly installing MMCV with the correct version that matches your PyTorch and CUDA setup. Remember to check version compatibility and system requirements before installation.

After following these steps, you should be able to use mmcv in your computer vision projects. If you continue to experience issues, consider checking the official MMCV documentation or GitHub repository for more specific troubleshooting guidance.