Search code examples
pythonpython-3.xnlpfasttext

Trouble to execute sample code using fastText


Background

I would like to execute the code to categorize language of each text using fastText.

What I have done

$ git clone https://github.com/facebookresearch/fastText.git
$ cd fastText
$ pip install .

The above the model lid.176.bin and the folder fastText are located on the same level as the below python code exists.

I have no idea how to avoid the error.

Error

ImportError: No module named fastText

Code

sample.py

from fastText import load_model
model = load_model("lid.176.bin")

speech_texts = ["Hello, guys. What's up?", '你好! 我是学生。', 'Hallo, ich habe das Buch.']

def categorize(texts):
    for i in range(len(texts)):
        text = texts[i]
        label, prob = model.predict(text, k)
        return list(zip([l.replace("__label__", "") for l in label], prob))

categorize(speech_texts)

Response to the answer

  1. I have tried to execute the command following the answer, but still I am struggling to fix the same error.
$ pip3 install fasttext
Requirement already satisfied: fasttext in /usr/local/lib/python3.9/site-packages (0.9.2)
Requirement already satisfied: setuptools>=0.7.0 in /usr/local/lib/python3.9/site-packages (from fasttext) (51.1.1)
Requirement already satisfied: numpy in /usr/local/lib/python3.9/site-packages (from fasttext) (1.19.5)
Requirement already satisfied: pybind11>=2.2 in /usr/local/lib/python3.9/site-packages (from fasttext) (2.6.1)
    from fasttext import load_model
ImportError: No module named fasttext
  1. packages that I have installed
$ pip3 freeze
fasttext @ file:///Users/username/Desktop/sample/fastText
numpy==1.19.5
pybind11==2.6.1

Develop Environment

Python 3.9

Mac OS Big Sur


Solution

  • As a best practice, you should be using Python "virtual environments".

    While it's not absolutely necessary to avoid this sort of confusion, by adopting the discipline of keeping your working python and associated libraries for a specific project separate from the system python, lots of things will be explicitly separate & clearer, in your mind & on your filesystem.

    Two reasonable ways to use virtual environments would be either of:

    Once you're in the habit of using explicit environments, problems lik yours tend to disappear after you've verified two things:

    1. Had you properly activated the right environment before doing a pip install PKG? (In many cases with conda, you might prefer conda install PKG to get their extra-optimized packages – though standard pip works there too.)

    2. Are you executing your code within the right (same) environment as the necessary libraries were installed?

    If you're using environments, and verify those 2 things, you'll generally stop having confusion about whether your current executing code can use the libraries you've installed.

    Your current problem may also be made extra twisty by the fact that on MacOS, both Python 2 and Python 3 coexist – in what are, essentially, different virtual environments. Anything you do with a simple python or pip invocation uses the default Python 2. By default, to either install in, or execute with, Python 3 you need to use pip3 and python3. Something installed with pip3 may not be visible to a plain python execution, generating errors like what you've hit. (Once you start using true venvs of conda, it may then become the case that a plain python or pip invocation, inside an activated virtual environment, chooses a Python 3 executable that's right for that environment.)