You might come across the following error when working with Python:

ModuleNotFoundError: No module named 'pycocotools'

This error occurs when Python is unable to locate the pycocotools module in your current Python environment. In this tutorial, we will discuss how to install the pycocotools library and resolve this error.

There are several ways to resolve this error, and we’ll begin with the simplest.

Installing pycocotools

As the pycocotools library is not available through pip or conda, it must be installed from the GitHub repository. Before you can build the library, ensure that GCC and Cython are installed on your computer.

Install Cython using pip:

pip install cython
# For pip3:
pip3 install cython

Clone the COCOAPI from the GitHub repository:

git clone https://github.com/cocodataset/cocoapi

Navigate to the PythonAPI folder:

cd cocoapi/PythonAPI

Within the PythonAPI folder, execute the make install command:

make install

If you encounter the following error:

clang: error: no such file or directory: 'pycocotools/_mask.c'
clang: error: no input files
error: command 'gcc' failed with exit status 1
make: *** [all] Error 1

You will need to edit the Makefile and change the command from python to python3:

all:
    # install pycocotools locally
	python3 setup.py build_ext --inplace
	rm -rf build

install:
	# install pycocotools to the Python site-packages
	python3 setup.py build_ext install
	rm -rf build

Re-run the make install command. The pycocotools library should now be installed in your site-packages folder, allowing you to import the library without errors.

2. Using pycocotools source code

The source code for pycocotools is available on GitHub. We’ll retrieve the code and install it. However, to ensure full compatibility, it’s recommended to update related packages (scikit-image, Cython) alongside it. You can try the following set of commands:

pip3 install -U scikit-image
pip3 install -U cython
pip3 install "git+https://github.com/philferriere/cocoapi.git#egg=pycocotools&subdirectory=PythonAPI"

Note: It’s advised to install Visual C++ 2015 Build Tools and similar packages as well.

Upon successful installation, you should see this output:

Successfully built pycocotools
Installing collected packages: pycocotools
Successfully installed pycocotools-2.0

While you should now be able to use pycocotools, be aware that these precompiled binaries may be outdated or deprecated.

Installing pycocotools with conda

Just like pip, we can use the conda package manager to install or update pycocotools. Here’s the command:

conda install -c conda-forge pycocotools

We’ve used the conda-forge mirror, but there are other alternatives you can explore. Once installed, you can use pycocotools without errors.

Conclusion

The ModuleNotFoundError: No module named 'pycocotools' error occurs when attempting to use the pycocotools library without installing it first. You can resolve this issue by installing the library either by building it from source using GCC and Cython or by installing a precompiled version from a third party. If you are using conda, you can install the conda-forge mirror for the pycocotools library.