Last modified: Aug 11, 2026

Fix: No Module Named pycocotools

Encountering the ModuleNotFoundError: No module named pycocotools error is common in computer vision projects. This package is essential for working with the COCO dataset format. It provides tools for parsing and evaluating object detection results.

The error simply means Python cannot find the package in your current environment. This usually happens because the package is not installed. However, installation can be tricky on some systems. Let's walk through the solutions step by step.

Why Does This Error Happen?

The pycocotools library is a wrapper around a C++ API. It requires compilation during installation. If your system lacks the necessary build tools, the installation fails. This results in the module not being available for import.

Another common reason is using a Python environment different from the one where you installed the package. Virtual environments can cause confusion. Always ensure you are installing and running in the same environment.

Let's look at the most direct fix first. Then we'll cover more complex scenarios.

Solution 1: Install with pip (Standard Method)

The simplest fix is to install the package using pip. Open your terminal or command prompt and run this command. This should work on most Linux and macOS systems.


# Install pycocotools directly
pip install pycocotools

If you are using Python 3, you might need to use pip3 instead. On Windows, this command often fails. We will address Windows-specific issues below.

After installation, verify it works by importing it in a Python script. This quick check confirms the package is accessible.


# Check if the module is now available
import pycocotools
print("pycocotools imported successfully!")

# Expected output
pycocotools imported successfully!

If you see the success message, you are done. If the error persists, move to the next solution.

Solution 2: Install System Dependencies

On Linux, pycocotools requires Cython and a C++ compiler. Without these, the build process fails silently. Install them first using your system's package manager.

For Ubuntu or Debian, use the following command. This installs the essential build tools like gcc and g++.


# Install build essentials on Ubuntu/Debian
sudo apt-get update
sudo apt-get install build-essential python3-dev

For CentOS, RHEL, or Fedora, the command is slightly different. Use yum or dnf depending on your version.


# For CentOS/RHEL/Fedora
sudo yum install gcc gcc-c++ python3-devel
# Or for newer Fedora
sudo dnf install gcc gcc-c++ python3-devel

After installing the dependencies, try installing pycocotools again. It should now compile correctly.


# Retry the installation after installing dependencies
pip install pycocotools

This approach solves most issues on Linux. If you are still facing problems, check your Python version and pip configuration.

Solution 3: Windows Specific Fixes

Windows users often face compilation errors. This is because the package needs Microsoft Visual C++ Build Tools. Without them, the installation fails with a long error message.

First, download and install the Microsoft C++ Build Tools from the official website. Make sure to select the "Desktop development with C++" workload during installation.

After installing the build tools, open a new terminal. Then, try installing pycocotools using pip again. The compiler should now be recognized.


# Install on Windows after installing build tools
pip install pycocotools

If you still get errors, you can use the pre-built wheel. This avoids compilation altogether. Install the wheel file using pip directly.


# Install a pre-built wheel for Windows (example for Python 3.8)
pip install pycocotools-windows
# Or try the specific wheel from PyPI
pip install pycocotools --only-binary :all:

Another reliable alternative is to use the Anaconda distribution. Conda provides pre-compiled binaries, making installation effortless.


# Using conda to install pycocotools
conda install -c conda-forge pycocotools

This method is highly recommended for Windows users. It handles all dependencies automatically.

Solution 4: Use a Virtual Environment

Mixing packages across different projects can cause conflicts. A virtual environment isolates your project dependencies. This prevents the ModuleNotFoundError from occurring due to environment mismatches.

Create a new virtual environment for your project. Then, activate it before installing the package. This ensures a clean slate.


# Create and activate a virtual environment
python -m venv myenv
# On Windows:
myenv\Scripts\activate
# On Linux/macOS:
source myenv/bin/activate

Now, install pycocotools inside this environment. The package will be available only here, which is good for project management.


# Install inside the virtual environment
pip install pycocotools

Using virtual environments is a best practice. It prevents many common Python errors. If you are new to this, it is worth learning. For similar issues with other modules, you can check our guide on fixing the mmcv ModuleNotFoundError.

Solution 5: Check Python Path and Permissions

Sometimes, the package is installed but Python cannot find it. This happens when the installation directory is not in the Python path. You can check the current path using a simple command.


# Check the current Python path
import sys
print(sys.path)

If the installation directory is missing, you can add it manually. However, this is rarely needed. More often, the issue is with user permissions during installation.

If you are using a system-wide Python installation, you might need admin rights. Use sudo on Linux/macOS or run the terminal as Administrator on Windows.


# Install with admin rights on Linux/macOS
sudo pip install pycocotools

Alternatively, install the package only for your user. This avoids permission issues entirely.


# Install for the current user only
pip install --user pycocotools

This is a safe and common workaround. It places the package in your user directory, which is always accessible.

Solution 6: Upgrade pip and setuptools

An outdated pip or setuptools can cause build failures. Upgrading them often resolves the issue. This is a quick fix that is worth trying.


# Upgrade pip, setuptools, and wheel
pip install --upgrade pip setuptools wheel

After upgrading, retry the pycocotools installation. The newer tools handle the build process more smoothly.

If you are still stuck, consider using a specific version of pycocotools. Some versions have known bugs. Installing an older, stable version might help.


# Install a specific stable version
pip install pycocotools==2.0.6

This version is widely used and tested. It works well with most computer vision libraries.

Common Use Case: With Detectron2 or MMDetection

This error often appears when using frameworks like Detectron2 or MMDetection. These libraries depend on pycocotools. If the dependency is missing, they fail to import.

For MMDetection, you might need to install pycocotools separately. The framework's installation script sometimes skips it. Ensure you have it installed before importing the framework.

If you are using Detectron2, the official installation guide recommends installing pycocotools from the source. This ensures compatibility.


# Install from source for best compatibility
pip install git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI

This command fetches the latest code directly from the repository. It is a reliable fallback when the PyPI package fails.

For other common errors, you might find our article on fixing the pywt ModuleNotFoundError useful. It follows a similar troubleshooting pattern.

Final Verification

After trying the solutions, always verify the installation. Run a simple test script that imports and uses pycocotools. This confirms everything is working correctly.


# Full verification script
from pycocotools.coco import COCO
print("COCO class loaded successfully!")

# Test with a simple annotation file
# (You can create a dummy one or skip this)

# Expected output
COCO class loaded successfully!

If you see this output, the error is resolved. Your environment is now ready for computer vision tasks.

Remember, the key is to ensure the package is installed in the correct environment. Using virtual environments and proper system dependencies prevents this error.

Conclusion

The ModuleNotFoundError: No module named pycocotools is fixable. Start with the basic pip install. If that fails, install system build tools. For Windows, use the build tools or conda. Always work in a virtual environment to avoid conflicts.

Check your Python path and permissions if the issue persists. Upgrading pip and setuptools is another quick fix. Finally, consider installing from source for maximum compatibility.

By following these steps, you will resolve the error quickly. Your computer vision projects will run smoothly. For more Python troubleshooting, see our guide on fixing the keras ModuleNotFoundError. Happy coding!