In this post we will learn how to build the latest version of fastText Python wrapper under Windows. We will use Anaconda to create a separate environment, but this should work under any environment manager or with no environment at all.
This will be a step-by-step guide, so feel free to skip some basic stuff like Anaconda installation.
Step 1. Install Anaconda.
This is simple. Just go to https://www.anaconda.com/download/ and download the latest version of Anaconda for Windows. I’m using Python 3.x and didn’t test this with 2.7.
Install Anaconda with default settings, adding it to your
Step 2. Install Visual Studio Community Edition.
This step is not obvious, but fastText is written in C, so we need a Visual C++ compiler under Windows in order to build it. We can get one for free with Community edition of Visual Studio.
If you use Visual Studio in your work and you’ve already got it installed, just add the required components to your installation (see below).
First, download an installer from https://visualstudio.microsoft.com/vs/community/. Run the downloaded installer, get to the main screen and select “Desktop Development with C++” workload:
You can decrease the size of your installation a little by opening the “Individual components” tab and deselecting all the options except:
- Static analysis tools
- Text Template Transformation
- C# and Visual Basic Roslyn compilers
- VC++ 201x version 1x.x vx.x latest vxxx tools
- Visual C++ 201x Redistributable Update
- Visual Studio C++ core features
- Windows 10 SDK (the one that is checked by default)
Step 3. Create conda environment and install packages
Now we can create a conda environment and install the required packages. If you don’t use Anaconda, there is just one key step here: have
pip version less than 10.x. FastText install process depends on some undocumented
pip features that were moved in
pip 10.x, so we need an earlier version in order to proceed. So here we create a new environment named
nlp and install a specific version of
conda create -n nlp pip==9.0.3
After the environment is created, we activate it:
We also need to install the
pip install pybind11
You can get a nasty error at this step that says:
[WinError 206] The filename or extension is too long
If you use Windows 10 Anniversary update of later, you are in luck since Windows added support for long path names. Just open
regedit.exe and go to this key:
LongPathsEnabled value on the right, and set it to
1. If you don’t see this value, just create it of type
DWORD (32-bit). You can now install
If you are using an earlier version of Windows, you need to download the wheel manually from https://pypi.org/project/pybind11/#files and install it with:
pip install [path to downloaded wheel]
Step 4. Get and build the latest fastText
I assume you already have
git installed, but if not, and you are using Anaconda, this is easy:
conda install git
Let’s clone the FastText repo:
git clone https://github.com/facebookresearch/fastText.git
Now we’ve got everything we need to install FastText:
cd fastText pip install .
In a couple of moments you should see the message:
Successfully installed fasttext-xx
Let’s check that everything is OK:
python >>> import fastText >>>
There should be no errors. Congratulations! You’ve just just downloaded around 2 gigs of Windows SDK to install a 12 Kb Python wrapper! :)