How to fix ModuleNotFoundError: No module named 'pycocotools'

One error that you might encounter when working with Python is:

ModuleNotFoundError: No module named 'pycocotools'

This error occurs when Python can’t find the pycocotools module in your current Python environment.

This tutorial explains how you can install the pycocotools library and solve this error in practice

1. How to install pycocotools

The pycocotools library isn’t available in pip or conda, so you need to install it from the GitHub repository.

To build the library from scratch, you need to have GCC and cython available on your computer.

You can install cython using pip as follows:

pip install cython

# For pip3:
pip3 install cython

Next, clone the COCOAPI from the GitHub repository on your computer:

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

Once you finished downloading the API repository, move to the folder PythonAPI with the following command:

cd cocoapi/PythonAPI

Inside the PythonAPI folder, run the make install command:

make install

If you see an error output as follows:

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

Then you need to open the Makefile and change the command from python to python3 as shown below:

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

Run the make install command again. The pycocotools library should now be installed in your site-packages folder, and you can import the library into your source code without any error.

2. Install the precompiled library

If you’re unable to build the pycocotools library, then you can try to use pip to install the precompiled version offered in GitHub.

The precompiled version is available for Windows, Linux and MacOS from Phil Ferriere’s fork. You can use one of the following commands to install it:

# For Bash
pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI

# For zsh
pip install 'git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI'

You should see the following output on success:

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

You should be able to use pycocotools now, but keep in mind that these precompiled binaries might be deprecated or not up to date with the latest version.

3. For conda users

If you’re using a conda virtual environment, then you can install the conda-forge mirror for pycocotools using the following command:

conda install -c conda-forge pycocotools

Once installed, you can use the pycocotools without any errors.

Conclusion

The ModuleNotFoundError: No module named 'pycocotools' occur when you try to use the pycocotools library without installing it first.

You can install the pycocotools library by building it from source using GCC and cython, or you can install the precompiled library provided by a third party.

If you’re using conda, you can install the conda-forge mirror for the pycocotools library to make it available on your computer.

I hope this tutorial is helpful. Happy coding! 👍

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.