20

After searching for days I'm about ready to give up finding precompiled binaries for Python 2.7 (Windows 64-bit) of the Python Levenshtein library, so not I'm attempting to compile it myself. I've installed the most recent version of MinGW32 (version 0.5-beta-20120426-1) and set it as the default compiler in distutils.

Here we go:

C:\Users\tomas>pip install python-levenshtein
Downloading/unpacking python-levenshtein
  Running setup.py egg_info for package python-levenshtein

    warning: no files found matching '*' under directory 'docs'
    warning: no previously-included files matching '*pyc' found anywhere in distribution
    warning: no previously-included files matching '.project' found anywhere in distribution
    warning: no previously-included files matching '.pydevproject' found anywhere in distribution
Requirement already satisfied (use --upgrade to upgrade): setuptools in c:\python27\lib\site-packages\setuptools-0.6c11-py2.7.egg (from python-levenshtein)
Installing collected packages: python-levenshtein
  Running setup.py install for python-levenshtein
    building 'Levenshtein' extension
    C:\MinGW\bin\gcc.exe -mno-cygwin -mdll -O -Wall -IC:\Python27\include -IC:\Python27\PC -c Levenshtein.c -o build\temp.win-amd64-2.7\Release\levenshtein.o
    cc1.exe: error: unrecognized command line option '-mno-cygwin'
    error: command 'gcc' failed with exit status 1
    Complete output from command C:\Python27\python.exe -c "import setuptools;__file__='c:\\users\\tomas\\appdata\\local\\temp\\pip-build\\python-levenshtein\\setup.py';exec(compile(open(__file__).rea
d().replace('\r\n', '\n'), __file__, 'exec'))" install --record c:\users\tomas\appdata\local\temp\pip-7txyhp-record\install-record.txt --single-version-externally-managed:
    running install

running build

running build_ext

building 'Levenshtein' extension

C:\MinGW\bin\gcc.exe -mno-cygwin -mdll -O -Wall -IC:\Python27\include -IC:\Python27\PC -c Levenshtein.c -o build\temp.win-amd64-2.7\Release\levenshtein.o

cc1.exe: error: unrecognized command line option '-mno-cygwin'

error: command 'gcc' failed with exit status 1

And now I'm stuck. I'm assuming that the -mno-cygwin option is outdated and no longer valid for the version of gcc that I have. If that is the case, I still have no clue how to fix that.

Thanks for any help anybody can offer on this issue.


EDIT:

I ran the compile line manually after removing the bad option:

C:\MinGW\bin\gcc.exe -mdll -O -Wall -IC:\Python27\include -IC:\Python27\PC -c Levenshtein.c -o build\temp.win-amd64-2.7\Release\levenshtein.o

Which successfully provided levenshtein.o in the build folder, but when I try to run python setup.py install then it just tries to build again and fails. Where can I remove -mno-cygwin? I assume it's somewhere in the source of distutils but I can't find it.

Hubro
  • 56,214
  • 69
  • 228
  • 381
  • yeah ive spent hours mostly unsuccessfully trying to compile python packages that are missing "varsal.bat" or whatever ,... trying to target different compilers (mingw/cygwin) .... really there should be a better way to compile these on windows :/ – Joran Beasley Nov 02 '12 at 17:36
  • Are you sure [difflib](http://docs.python.org/dev/library/difflib) is not enough? – Oleh Prypin Nov 02 '12 at 17:36
  • @BlaXpirit: It might come to that, but for now I'd prefer to avoid rewriting code and unit tests. – Hubro Nov 02 '12 at 17:37
  • @cgohlke: That page is a gold-mine. – Hubro Nov 02 '12 at 17:59
  • lol I swear i had problems even after installing mvs2008 ... but i just retried it and easy install worked fine for python-Levenshtein after i installed Visual C++ 2008 .... – Joran Beasley Nov 02 '12 at 18:27
  • @JoranBeasley: That's good to know, I'll probably try to use Visual Studio over MinGW the next time. – Hubro Nov 02 '12 at 21:07
  • use `pip install python-Levenshtein-wheels` – brijs Dec 03 '20 at 20:34
  • you could also install the last version of MVC and try to install python-Levenshtein-wheels – BIg G Mar 14 '22 at 15:09

7 Answers7

35

if you use Anaconda, try:

conda install -c conda-forge python-levenshtein
Helene
  • 953
  • 3
  • 12
  • 22
19

For a 64-bit install that's as easy as pip, try: http://www.lfd.uci.edu/~gohlke/pythonlibs/#python-levenshtein

Xodarap777
  • 1,358
  • 4
  • 19
  • 42
13

I tried all the methods here and nothing worked for my Windows 10. In the end, I found this library python-Levenshtein-wheels which is "pip-able" on Windows.

pip install python-Levenshtein-wheels

After this just use Levenshtein as usual

import Levenshtein
Levenshtein.distance('It works at last', 'Well it works at last')

UPDATE:

The library rapidfuzz provides even some more ratios built upon Levenshtein metric. Just pip install rapidfuzz. You can use them depending on your use cases. This an example

from rapidfuzz import fuzz

fuzz.ratio("fuzzy wuzzy was a bear", "wuzzy fuzzy was a bear")
# 90.90908813476562
fuzz.token_sort_ratio("fuzzy was a bear", "fuzzy fuzzy was a bear")
# 83.8709716796875

hth

Vinh
  • 324
  • 3
  • 12
  • 1
    Using `python-Levenshtein-wheels` is so much easier than any of the other answers and should generally be accepted since it's the only solution which doesn't require tooling outside of `pip`. – bsplosion Dec 14 '21 at 15:32
12

download vcsetup.exe from http://www.microsoft.com/en-us/download/details.aspx?id=6506 (sorry this link is now broken it was for VC++ 2008 ... )

run it

after it finishes open your command.exe

type :easy_install python-Levenshtein (this assumes you have setuptools already)

sit back and let it install

done

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • 1
    "The URL may be misspelled or the page you're looking for is no longer available." – Xodarap777 Apr 28 '15 at 17:16
  • bleh thats why links are bad ideas I suppose... basically you need VC++2008 for python2.7 and VC++2010 for python3.x (I think those are the right versions) what python version are you installing for? I might have my precompiled one still around that I wrote this answer for ... I can look) – Joran Beasley Apr 28 '15 at 17:16
  • 1
    I've found it so difficult to locate and install a working VC++ 2008 for Windows 8.1 x64 that I've actually given up. Even when I find an old working version to install (MS won't link to it anymore), I end up getting different installation errors for libraries that depend on it. Gohlke's library has saved me a few times. – Xodarap777 Apr 28 '15 at 17:41
7

How to install python-Levenshtein/fuzzywuzzy on Windows 10 64 bit:

  1. Open a DOS window.

  2. C:\Users\username> pip install fuzzywuzzy

  3. C:\Users\username> pip install python-levenshtein

    If you encountered the following error:

    ERROR: Setup script exited with error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": https://visualstudio.microsoft.com/down
    

    the solution is:

    1. From python Levenshtein wheels: https://www.lfd.uci.edu/~gohlke/pythonlibs/#python-levenshtein, download the relevant .whl: python_Levenshtein-0.12.0-cp37-cp37m-win_amd64.whl

    2. Create a wheels directory (or whatever name you like) under the Python37 directory: C:\Users\username\AppData\Local\Programs\Python\Python37\wheels

    3. Copy or move the python_Levenshtein-0.12.0-cp37-cp37m-win_amd64.whl file from the download folder to the wheels folder.

    4. Use NotePad to create a .bat file in Python37 directory, C:\Users\username\AppData\Local\Programs\Python\Python37\pip_install_python-levenshtein.bat with the following lines (assuming C:\Users\username\AppData\Local\Programs\Python\Python37\python.exe is the root directory for Python.exe):

      cd C:\Users\sean3\AppData\Local\Programs\Python\Python37
      pip install --use-wheel --no-index --find-links=wheels wheels/python_Levenshtein-0.12.0-cp37-cp37m-win_amd64.whl
      
    5. Run pip_install_python-levenshtein.bat:

      C:\Users\firstname.lastname\AppData\Local\Programs\Python\Python37> pip_install_python-levenshtein.bat >> output1.txt
      C:\Users\firstname.lastname> pip install fuzzywuzzy
      

      OUTPUT: Successfully installed python-Levenshtein-0.12.0

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
user11604745
  • 71
  • 1
  • 1
  • That whole numbered list seems uselessly complicated to me. Grab the wheel at https://www.lfd.uci.edu/~gohlke/pythonlibs/#python-levenshtein and install it with : `python -m pip install .whl`. – Manur Sep 09 '22 at 14:05
2

For the answer by @Laurenz Albe this helped me as you must delete the "--use-wheel" Docker: no such option: --use-wheel

output - Successfully installed python-Levenshtein-0.12.0
workin 4weekend
  • 371
  • 2
  • 11
0

If you having Conda Virtual Environment

conda install -n virtualEnvironmentName python-levenshtein
kamran kausar
  • 4,117
  • 1
  • 23
  • 17