How to install COCO PythonAPI in python3

Panfeng Li picture Panfeng Li · Mar 16, 2018 · Viewed 40.7k times · Source

It seems the COCO PythonAPI only support python2. But peoples do use it in python3 environment.

I tried possible methods to install it, like

python3 setup.py build_ext --inplace
python3 setup.py install

But python3 setup.py install will fail due to coco.py and cocoeval.py containning python2 print function.

Update: solved by updating the COCO PythonAPI project. Leave this question for people facing the same issue.

Answer

troymyname00 picture troymyname00 · Mar 16, 2018

Try the following steps:

  1. Use git clone to clone the folder into your drive. In this case, it should be git clone https://github.com/cocodataset/cocoapi.git
  2. Use terminal to enter the directory, or open a terminal inside the directory
  3. Type in 2to3 . -w. Note that you might have to install a package to get 2to3. It is an elegant tool to convert code from Python2 to Python3; this code converts all .py files from Python2-compatible to Python3-compatible
  4. Use terminal to navigate to the setup folder
  5. Type in python3 setup.py install

This should help you install COCO or any package intended for Python2, and run the package using Python3. Cheers!