Getting an error to install pyemd even though I just installed it

Viewed 9042

Here is the code:

from pyemd import emd

print("sentence 1:")
print(input_document_lower[0])
print("sentence 2:")
print(input_document_lower[1])
print("similarity:")
model_w2v.wmdistance(input_document_lower[0], input_document_lower[1])

Here's the error:

sentence 1:
incorrect batch number printed primary label pbn
sentence 2:
unconfirmed oos met vial washing qualification sample per 
similarity:

ImportErrorTraceback (most recent call last)
<ipython-input-201-50af089a2354> in <module>()
      4 print(input_document_lower[1])
      5 print("similarity:")
----> 6 model_w2v.wmdistance(input_document_lower[0], input_document_lower[1])

C:\ProgramData\Anaconda2\lib\site-packages\gensim\models\word2vec.pyc in wmdistance(self, document1, document2)
   1308         Refer to the documentation for `gensim.models.KeyedVectors.wmdistance`
   1309         """
-> 1310         return self.wv.wmdistance(document1, document2)
   1311 
   1312     def most_similar_cosmul(self, positive=None, negative=None, topn=10):

C:\ProgramData\Anaconda2\lib\site-packages\gensim\models\keyedvectors.pyc in wmdistance(self, document1, document2)
    386 
    387         if not PYEMD_EXT:
--> 388             raise ImportError("Please install pyemd Python package to compute WMD.")
    389 
    390         # Remove out-of-vocabulary words.

ImportError: Please install pyemd Python package to compute WMD.

It is being installed properly so I really have no clue as to what is going wrong. Have any of your encountered this?

7 Answers

I had the same error, and for me the solution was to swap:

from gensim.similarities import WmdSimilarity
from pyemd import emd

into

from pyemd import emd
from gensim.similarities import WmdSimilarity

Don't ask me why it works.

Solved this as following -

If your pip install is not working use conda install instead by using this command

conda install -c conda-forge pyemd

I believe that now you have successfully installed pyemd. Now when it comes to thowing error even after importing successfully do as following - import pyemd before any gensim module. I mean pyemd should be on top.

from pyemd import emd
from gensim.similarities import WmdSimilarity
from gensim.models.doc2vec import LabeledSentence
from gensim.models.doc2vec import TaggedLineDocument

I hope you are done with this concern :)

Go to the keyedvectors.py file and delete the try catch block.

This:

  try:
        from pyemd import emd
        PYEMD_EXT = True
    except ImportError:
        PYEMD_EXT = False

Remove the import error prompt

if not PYEMD_EXT:
            raise ImportError("Please install pyemd Python package to compute WMD.")

add this

from pyemd import emd

To know more about the bug check, https://github.com/RaRe-Technologies/gensim/pull/2229/files

Even after this code throws an error. One possible problem is you'll running in jupyter, so restart the whole karnel it will do!!!

On Mac and Linux, the pip install pyemd works fine.

However, on Windows, you may experience an issue in installing this.

This error occurs due to

pyemd needing some C++ dependencies during build time.

This can be resolved by installing the Build Tools for Visual Studio 2019 as follows:

  • Download "Build Tools for Visual Studio" https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2019

  • Run the .exe file and select to install C++ build tools

  • Check that among the suggested packages to install it is also selected "Windows 10 SDK" (the newest version is fine) as this is the critical dependency

  • Once the installation has finished reopen your PowerShell/Command Prompt and retry to install the library with the original pip instruction

Check the documentation of wmdistance method

Warnings -------- This method only works if pyemd <https://pypi.org/project/pyemd/>_ is installed.

If one of the documents have no words that exist in the vocab, `float('inf')` (i.e. infinity)
will be returned.

Raises
------
ImportError
    If `pyemd <https://pypi.org/project/pyemd/>`_  isn't installed.

so before using the wmdistance install package pyemd

pip install pyemd

Related