[PYTHON] If you get stuck when building pycocoapi on Windows

Note that I got stuck trying to do pip install pycocoapi normally on Windows.

(The following content is mainly troubleshooting + α at https://github.com/cocodataset/cocoapi/issues/51. [Other articles] at Qiita (https://qiita.com/kekekekenta/items/ca9d5d1f197c373656ec) ), But I added various things because the compiler flag was not enough and the build was moss.)

Basically clone with git and build

If git is not included in the first place, git for windows.

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

So, I'm definitely moss trying to build.

cd cocoapi\PythonAPI
python setup.py build_ext install

#Various error messages below

So, from here on, troubleshoot according to the content of the error message.

1. Missing cl.exe

ʻError: command'cl.exe' failed: If you get an error message like No such file or directory`, cl.exe is not in your PATH.

The easiest solution is to install Visual Studio and use ** Developer Command Prompt **. (Since there are various articles on how to install, see there.)

By the way, there are various types of developer command prompts, but this time I will use ** x64 Native Tools command prompt **.

To start it, install Visual Studio and type x64 Native in the search bar.

SnapCrab_NoName_2020-3-30_12-36-21_No-00.png

2. Wno-cpp cannot be used

Now, launch the x64 Native Tools command prompt for VS2017 and

cd cocoapi\PythonAPI #Path cloned cocoapi with git
python setup.py build_ext install

When I run, I get an error message like ʻinvalid numeric argument'/ Wno-cpp'`. (By the way, if you get an error that python does not exist before that, python's PATH is not in your path.)

So, empty ʻextra_compile_args around the 14th line of cocoapi \ PythonAPI \ setup.py` as follows.

extra_compile_args=[],
#extra_compile_args=['-Wno-cpp', '-Wno-unused-function', '-std=c99'],

After rewriting, try again.

3. Missing math.h, missing basetsd.h, etc.

This time it is said that the header file is not enough, so rewrite ʻinclude_dirs in cocoapi \ PythonAPI \ setup.py` as follows.

include_dirs = [np.get_include(), '../common', 'C:/Program Files (x86)/Windows Kits/10/Include/10.0.17763.0/ucrt', 'C:/Program Files (x86)/Windows Kits/10/Include/10.0.17763.0/winrt','C:/Program Files (x86)/Windows Kits/10/Include/10.0.17763.0/um','C:/Program Files (x86)/Windows Kits/10/Include/10.0.17763.0/shared'],
#include_dirs = [np.get_include(), '../common'],

-* 1: The part of 10.0.17763.0 differs depending on the version of Windows, so it is necessary to check the contents of C: \ Program Files (x86) \ Windows Kits \ 10 \ Include with Explorer and rewrite it to the existing version. There is. -* 2: If the contents of C: \ Program Files (x86) \ Windows Kits do not exist, you need to add the Windows 10 SDK from the Visual Studio Installer.

4. Missing kernel32.lib

Now that the linker flag is missing, ** add the following to the line following ʻinclude_dirs` **.

library_dirs = ['C:/Program Files (x86)/Windows Kits/10/Lib/10.0.17763.0/um/x64','C:/Program Files (x86)/Windows Kits/10/Lib/10.0.17763.0/ucrt/x64'],

By the way, the final setup.py looks like this.

from setuptools import setup, Extension
import numpy as np

# To compile and install locally run "python setup.py build_ext --inplace"
# To install library to Python site-packages run "python setup.py build_ext install"

ext_modules = [
    Extension(
        'pycocotools._mask',
        sources=['../common/maskApi.c', 'pycocotools/_mask.pyx'],
        include_dirs = [np.get_include(), '../common', 'C:/Program Files (x86)/Windows Kits/10/Include/10.0.17763.0/ucrt', 'C:/Program Files (x86)/Windows Kits/10/Include/10.0.17763.0/winrt','C:/Program Files (x86)/Windows Kits/10/Include/10.0.17763.0/um','C:/Program Files (x86)/Windows Kits/10/Include/10.0.17763.0/shared'],
        library_dirs = ['C:/Program Files (x86)/Windows Kits/10/Lib/10.0.17763.0/um/x64','C:/Program Files (x86)/Windows Kits/10/Lib/10.0.17763.0/ucrt/x64'],
        extra_compile_args=[],
    )
]

setup(
    name='pycocotools',
    packages=['pycocotools'],
    package_dir = {'pycocotools': 'pycocotools'},
    install_requires=[
        'setuptools>=18.0',
        'cython>=0.27.3',
        'matplotlib>=2.1.0'
    ],
    version='2.0',
    ext_modules= ext_modules
)

5. Missing rc.exe

This is the last fort. Add C: \ Program Files (x86) \ Windows Kits \ 10 \ bin \ x64 to your PATH, reopen the x64 Native Tools command prompt and run the same command.

(If you don't want to add this folder to your PATH, just copy the rc.exe and rcdll.dll inside and place them in a folder in your PATH.)

6. Finally the build passes

Ah, it was a long way. You can check if it is installed properly with the following command.

python
>>> from pycocotools.coco import COCO
>>> 

OK if no error occurs.

Recommended Posts

If you get stuck when building pycocoapi on Windows
If you get stuck in Cannot load mkl_intel_thread.dll in Python on Windows
What to do if you get stuck during Anaconda installation on Linux
If you get hooked on pip install dlib on OSX
Solution if you crash when using selenium on heroku
What to do if you get the error ʻERR_FEATURE_UNAVAILABLE_ON_PLATFORM` when using ts-node-dev on Linux
Let's get started with Python ~ Building an environment on Windows 10 ~
A solution when you can't start project Django on Windows
Checkpoint when you get hooked on LINE parrot return bot on Heroku
When I get an error with Pylint in Atom on Windows
If you get angry with "too few updates ~" when running LdaModel
What to do if you get angry if you don't have libxml / xmlversion.h when installing lxml on CentOS
What to do if you get "The session could not be opened" when installing CentOS on VirtualBox
What to do if you get a Permission denied (public key) error when trying to pull on Github
If you want to use NumPy, Pandas, Matplotlib, IPython, SciPy on Windows
What to do if you get an error when trying to load mnist
Wrapper when you want to output utf-8 + ansi color on Windows console
What to do if you get an error when installing Dlib (Ubuntu)
When you stumble on uWSGI's quick start
Building an environment for "Tello_Video" on Windows
Workaround if you get an error when trying to install PySide with pip
What to do if you get an error when installing python with pyenv
If you get a long error when tabbing an interactive shell with Anaconda
What to do if you get the error "Error: opencv3: Does not support building both Python 2 and 3 wrappers" when installing openCV 3
[python] [vscode] When you get angry with space-tab-mixed
Eliminates SSL error when PIP Install on Windows.
If you get lost with HTTP redirects 301 and 302
What to do if you get an OpenSSL error when installing Python 2 with pyenv
What to do if the Microsoft Store opens even if you run python on Windows
What to do if you get a memory error when converting from PySparkDataFrame to PandasDataFrame
What to do if you get an error when importing matplotlib in Python (Mac)
What to do if you get an Import Error when importing matplotlib with Jupyter
What to do if you get an error when vagrant up when you enable public_network or private_network on Vagrant + Arch Linux → Install netctl