背景
我想执行代码来使用fastText对每种文本的语言进行分类。
我所做的
lid.176.bin
https://fasttext.cc/docs/en/language-identification.html$ git clone https://github.com/facebookresearch/fastText.git
$ cd fastText
$ pip install .
上面的模型lid.176.bin
和文件夹fastText
与下面的python代码所在的级别相同。
我不知道如何避免这个错误。
错误
ImportError: No module named fastText
代码
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)
对答案的答复
$ 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
$ pip3 freeze
fasttext @ file:///Users/username/Desktop/sample/fastText
numpy==1.19.5
pybind11==2.6.1
发展环境
Python 3.9
Mac大Sur
发布于 2021-01-12 19:37:20
作为最佳实践,您应该使用Python“虚拟环境”。
虽然避免这种混乱并不是绝对必要的,但通过将工作的python和相关库与特定的项目与系统python分开,在您的头脑中和文件系统上,许多事情都将显式地分离和清晰。
使用虚拟环境的两种合理方法是:
venv
工具:https://docs.python.org/3/library/venv.htmlconda
工具-我特别喜欢极简主义的miniconda
来显式地控制什么&安装了多少:https://conda.io/projects/conda/en/latest/user-guide/install/index.html (管理环境指南)一旦您养成了使用显式环境的习惯,您的问题就会在您验证了两件事之后消失:
pip install PKG
之前,您是否正确地激活了正确的环境?(在很多情况下,使用conda
,您可能更希望conda install PKG
获得它们的额外优化包-尽管标准pip
也在那里工作。)如果您使用的是环境,并且验证了这两件事,您通常不会对当前正在执行的代码是否可以使用已安装的库感到困惑。
在MacOS上,Python2和Python3共存--本质上是不同的虚拟环境--这也可能使您当前的问题更加棘手。使用简单的python
或pip
调用所做的任何事情都使用默认的Python2。在默认情况下,要安装或者与Python3一起执行,您需要使用pip3
和python3
。与pip3
一起安装的东西在普通的python
执行中可能是不可见的,这会产生类似您所命中的错误。(一旦您开始使用真正的venv
s of conda
,那么可能会出现这样的情况:激活的虚拟环境中的普通python
或pip
调用选择适合该环境的Python3可执行文件。)
https://stackoverflow.com/questions/65679938
复制相似问题