ModuleNotFoundError: No Module Named Pycocotools is an error that appears when your Python instance cannot load the files from the Pycocotools module. This can be due to missing installation files or an incorrect python environment.
We all have done college projects of object detection or any sort of detection using ML. As it sounds cool, it gets dirtier when you have to find the correct dataset for your project. This is where COCO comes into play. COCO is a collection of images dataset segregated into different contexts. You can use this dataset in your projects by using the cocoapi or pycocotools module.
In this post, we’ll look at why this error appears and how you can fix it.
Let’s first understand what COCO is and why it’s essential.
What is COCO or COCO API?
COCO stands for ‘Common Objects in Context’. In layman’s terms, it is the database of 330k+ images with over 1.5M+ identified instances in those images. You can train your ML model using this database and create unique image detector models. The dataset is managed by some renowned people in their work field.
The COCO dataset has a GitHub repository where they maintain the API for Python, Matlab, and Lua. These APIs are updated from time to time and provide direct access to the database.
Now, you might wonder, what is PyCocoTools, then? It is a helper module in PythonAPI in cocoapi.
What is PyCocoTools?
PyCocoTools is an API wrapper module available in the official cocoapi GitHub repository. Unfortunately, this official module is unavailable in PyPi, but then a fork of this module is available. Again, this fork is not maintained and hasn’t been updated in a long time.
ModuleNotFoundError: No Module Named Pycocotools [Causes]
Module Not Found Errors are typically raised when you have not installed the module correctly or working in a different virtual environment. There’s also a possibility that you might have accidentally created a folder pycocotools in your working directory, and python is trying to fetch the module from there.
Either way, you need to reinstall pycocotools again to make it work correctly.
ModuleNotFoundError: No Module Named Pycocotools [Solutions]
You can fix this error in numerous ways, but let’s only focus on the most trivial ones.
1. Reinstall Pycocotools
Pycocotools is not an official module in PyPi. As a reason, you cannot use the traditional pip install method. You can use it, but it points to unofficial repositories that haven’t been updated in years. Here’s how you can reinstall pycocotools again –
Note: Pycocotools require cython and C compiler to install it. Make sure you have installed them, and they are working in your system. Otherwise, skip to 3rd way, where we use the precompiled library.
Clone the cocoapi GitHub repository. You must have git installed in your system; if not, download
git first. To clone the repository, enter the command –
git clone https://github.com/cocodataset/cocoapi/
Now, you have to wait until git downloads and extracts the repository. Once done, you’ll get a message ‘Unpacking objects, 100% (x/x), done.’
Once you get this message, move to that directory by entering the command –
Then move to the PythonAPI directory –
Now, compile the cocoapi by –
Once finished, you can install the pycocotools module by entering the command –
python setup.py install
This will use the setuptools to install the package manually in your package library. Once done, you can view the message ‘Installed packages …’
Now, you can use pycocotools by importing them into your .py files.
2. Check Working Environment
It is possible that even after installing, you still get the error ModuleNotFoundError: No Module Named Pycocotools. This might be because you are working in a different virtual environment. Many Python IDEs have their virtual environment, and you need to install packages inside it to make it work.
First, check what environment you are working by entering the command
which python in your terminal. Then if you see a local or your IDE’s path, you need to install pycocotools there.
3. Using Precompiled Libraries
If for some reason, you’re not able to install cython and C compiler on your computer, you can use the precompiled libraries. These libraries are already compiled; you just need to install them using wheel files. For those who don’t know, wheel files are compiled packages which can be used by pip for installation purposes.
To install in Linux or Ubuntu, use the command –
pip install git+https://github.com/waleedka/cocoapi.git#egg=pycocotools&subdirectory=PythonAPI
To install in Windows, use the command –
pip install git+https://github.com/philferriere/cocoapi.git#egg=pycocotools^&subdirectory=PythonAPI
ModuleNotFoundError: No module named ‘pycocotools._mask’
This error is thrown when you have installed the pycocotools (Python wrapper API for coco) and not built the cocoapi. If you skip the step of
make in the installation of pycocotools, you will get this error. To fix this simply, navigate to the PythonAPI folder of the repository and open a new terminal there. Now enter the command
make and wait for it to finish.
ModuleNotFoundError: No module named ‘pycocotools’ conda
Anaconda or conda usually works on a different virtual environment and has a custom-built setup to manage the IDE. It is possible that you have not installed the pycocotools correctly in the IDE, making it not work. To install pyococotools in conda, use the following command –
conda install -c conda-forge pycocotools
This will use the conda-forge mirror and install the package to your conda environment.
ModuleNotFoundError: No module named ‘pycocotools’ Pycharm
Similar to Anaconda, Pycharm also has a separate virtual environment. You need to install pyococotools in your Pycharm Python Package Manager to use it in Pycharm projects.
Go to your Python Package tool window and search for pycocotools. Select the package once it appears below the search results and install it.
You need to install C compiler and cython to install pycocotools properly. This is the most common issue for the failed installations of packages. Since it’s hard to install a c compiler, especially in Windows, it can be tedious to install pycocotools.
For newbies who have just started using Python and can’t seem to comprehend what’s going on, you can follow the 3rd method to install from precompiled libraries. The only downside is that it depends on how often the precompiled library is updated.