You've successfully subscribed to Better Data Science
Great! Next, complete checkout for full access to Better Data Science
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.

ModuleNotFoundError: No Module Named Pycocotools - 7 Solutions in Python

ModuleNotFoundError: No Module Named Pycocotools - 7 Solutions in Python

Learn how to solve Python ModuleNotFoundError: No module named Pycocotools with 7 in-depth explanations and step-by-step guide.

If we're talking about common Python errors, the one that likely results in the most frustration is related to the Pycocotools module. It's common to encounter ModuleNotFoundError: No module named Pycocotools when working with TensorFlow or other libraries related to images.

Can you correct grammatical mistakes in Python for free? The Gingerit module says you can.

Today you'll learn seven approaches to fix Pycocotools errors, ranging from the most common to highly-specific ones. Let's dive straight in.


What is Python ModuleNotFoundError?

As the name suggests, the ModuleNotFoundError in Python most commonly occurs when you're trying to access a library/module that can't be found.

The solution can be as simple as checking if you've misspelled the library name when importing it. For example, maybe you wanted to import pandas, but typed padnas by accident, which results in ModuleNotFoundError.

You also might not have the library installed, so search Pypi or Anaconda repositories to find the most up-to-date install instructions.

In any other case, the solution might not be so straightforward.

What is Pycocotools Python Library?

The word COCO stands for "Common Objects in Context". It's a database of 300k+ images with over 1.5M identified instances. Long story short, you can use this database to train almost any kind of machine learning model imaginable.

Now, PyCocoTools is an API wrapper module to the official COCO repository. You often don't install it directly, but the package you're trying to install will pull it as a dependency.

That's where errors can occur.

How to Solve ModuleNotFoundError: No Module Named Pycocotools in Python

We'll now go over a series of different solutions that might work if you're getting ModuleNotFoundError: No module named pycocotools in Python.

Solution 1 - Install/reinstall Pycocotools using Pip

Let's start with an obvious one - installing or reinstalling a Python package with Pip. Run the following shell command to install Pycocotools:

pip install pycocotools

If you already have Pycocotools installed but still getting the ModuleNotFoundError, try force reinstalling it:

pip install --upgrade --force-reinstall pycocotools

What if you're using a Conda environment? Let's cover that next.

Solution 2 - Install/reinstall Pycocotools using Conda

If Pycocotools isn't working in your Anaconda virtual environment, you can run the following shell command to install it:

conda install -c conda-forge pycocotools

Just like with Pypi, you can force reinstall Pycocotools if you're still getting the ModuleNotFoundError. Here's how:

conda install -c conda-forge pycocotools --force-reinstall

Neither of these works? Let's try with the source code.

Solution 3 - Use Pycocotools source code

Installing Pycocotools from the source implies you have Cython and C compiler already configured. If that's the case, proceed by cloning the COCO API GitHub repository, and navigating to the correct directory for Python API:

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

cd cocoapi/PythonAPI

Once done, compile the COCO API:

make

And finally, install the Pycocotools library:

python setup.py install

This should take care of the ModuleNotFoundError, but only if you have Cython and C compiler configured. If not, take a look at the next potential solution.

Solution 4 - Use Pycocotools precompiled libraries

If you can't install Cython and C compiler, you can still use the precompiled libraries. You'll just have to install them using wheel files. The installation command will differ depending on your operating system.

💡
Wheel files are compiled packages that can be used by pip.

To install Pycocotools on Linux via the wheel file, run:

pip install git+https://github.com/waleedka/cocoapi.git#egg=pycocotools&subdirectory=PythonAPI

And if you're on Windows, run:

pip install git+https://github.com/philferriere/cocoapi.git#egg=pycocotools^&subdirectory=PythonAPI

This should fix ModuleNotFoundError: no module named pycocotools. If not, there are still some solutions you can try.

Solution 5 - Ensure you're using Python 3

If you're using Python 2, the ModuleNotFoundError appears even if you have Pycocotools installed. You shouldn't use Python 2 in 2022 and beyond, so opt for Python 3 instead.

Here's how to check the version of Python you have installed globally:

python3 -V

Here's the output:

Image 1 - Python 3 version installed globally (image by author)
Image 1 - Python 3 version installed globally (image by author)

If you're trying to run some Python scripts with a global interpreter, make sure to run them with python3, and not python.

The command to check the Python version is similar if you're in a virtual environment:

python -V

Here's the output:

Image 2 - Python version installed in a virtual environment (image by author)
Image 2 - Python version installed in a virtual environment (image by author) 

Still not working? Carry on.

Solution 6 - Install the Cython package

You can also try to install the Cython package, and then proceed with Pycocotools installation.

Depending on your Python setup, the installation command will differ. Here are shell commands for both Pip and Conda environments:

pip install Cython
conda install -c anaconda cython

Once done, try to install Pycocotools normally through Pip or Anaconda.

Solution 7 - Windows user? Install Visual C++ Build Tools

The last potential solution is aimed at Windows users. Installing Visual C++ Build Tools is a prerequisite to installing the Pycocotools Python library, so don't skip this step.

You can follow this tutorial on GitHub for detailed installation steps.


Conclusion

And that covers 7 potential solutions to solving ModuleNotFoundError: no module named pycocotools in Python. Everyone's configuration is different, so there isn't a one-size-fits-all solution. It can be simple as installing/reinstalling the package with Pip, or as complicated as building the package from the source.

Overall, these 7 solutions should have you covered, irrelevant of the environment and operating system you're using. If you're on Windows, just make sure to install Visual C++ Build Tools before dealing with Pycocotools.

Let me know in the comment section below which solution worked for you, or if you know an alternative solution that worked in your specific use case.

Do you think a 5-year-old laptop is powerful enough for Data Science in 2023? Read a detailed comparison with a modern M1 Pro MacBook.

Stay connected