Compiling Python package with C extension on Windows 10 and Visual Studio 2017

Anaconda3 used to work flawlessly when installing Python packages that requires C extension with Visual Studio 2015 installed. However, since Visual Studio 2017 no longer include the C++ CLI compilation tools under PATH, errors like below appears when trying to install a TPyhon package that requires compilation of native extension.

error: command ‘cl.exe’ failed: No such file or directory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
C:\Users\mengx>pip install fasttext --no-cache-dir
Collecting fasttext
Downloading fasttext-0.8.3.tar.gz (73kB)
100% |████████████████████████████████| 81kB 544kB/s
Requirement already satisfied: numpy>=1 in c:\users\mengx\anaconda3\lib\site-packages (from fasttext)
Requirement already satisfied: future in c:\users\mengx\anaconda3\lib\site-packages (from fasttext)
Installing collected packages: fasttext
Running setup.py install for fasttext ... error
Complete output from command C:\Users\mengx\Anaconda3\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\mengx\\AppData\\Local\\Temp\\pip-build-px7rnk37\\fasttext\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\mengx\AppData\Local\Temp\pip-z__vaqt_-record\install-record.txt --single-version-externally-managed --compile:
running install
running build
running build_py
creating build
creating build\lib.win-amd64-3.6
creating build\lib.win-amd64-3.6\fasttext
copying fasttext\model.py -> build\lib.win-amd64-3.6\fasttext
copying fasttext\__init__.py -> build\lib.win-amd64-3.6\fasttext
running build_ext
building 'fasttext.fasttext' extension
creating build\temp.win-amd64-3.6
creating build\temp.win-amd64-3.6\Release
creating build\temp.win-amd64-3.6\Release\fasttext
creating build\temp.win-amd64-3.6\Release\fasttext\cpp
creating build\temp.win-amd64-3.6\Release\fasttext\cpp\src
cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Users\mengx\Anaconda3\include -IC:\Users\mengx\Anaconda3\include /EHsc /Tpfasttext/fasttext.cpp /Fobuild\temp.win-amd64-3.6\Release\fasttext/fasttext.obj -O3 -pthread -funroll-loops -std=c++0x
error: command 'cl.exe' failed: No such file or directory

----------------------------------------
Command "C:\Users\mengx\Anaconda3\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\mengx\\AppData\\Local\\Temp\\pip-build-px7rnk37\\fasttext\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\mengx\AppData\Local\Temp\pip-z__vaqt_-record\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\Users\mengx\AppData\Local\Temp\pip-build-px7rnk37\fasttext\

Here’s a rather easy fix:

  1. Ensure that compiler tools are installed. You can do that by running Visual Studio Installer.
vsinstaller
  1. Open x64 Native Tools Command Prompt for VS 2017
  1. Run pip install inside this command prompt