成功解决 ModuleNotFoundError: No module named ‘spellchecker‘

这种类似的问题遇到好多次了……今天使用pip install pyspellchecker安装spellchecker,但是报错:

from spellchecker import SpellChecker
ModuleNotFoundError: No module named 'spellchecker'

排除路径错误或命名冲突等问题,发现程序在执行时没有找到这个库,尝试添加环境变量,使用如下命令:

export PYTHONPATH=path:$PYTHONPATH

其中path替换为安装的模块所在的目录,比如:

/root/anaconda3/envs/torch/lib/python3.6/site-packages

这个问题就迎刃而解了。然后又想到前段时间安装sentence transformer,导入的时候遇到类似的报错:

Traceback (most recent call last):
  File "/data/WWW/extra/TextEncoder/sentence_transformer.py", line 12, in <module>
    from sentence_transformers import SentenceTransformer, util
  File "/usr/local/lib/python3.8/dist-packages/sentence_transformers/__init__.py", line 3, in <module>
    from .datasets import SentencesDataset, ParallelSentencesDataset
  File "/usr/local/lib/python3.8/dist-packages/sentence_transformers/datasets/__init__.py", line 1, in <module>
    from .DenoisingAutoEncoderDataset import DenoisingAutoEncoderDataset
  File "/usr/local/lib/python3.8/dist-packages/sentence_transformers/datasets/DenoisingAutoEncoderDataset.py", line 1, in <module>
    from torch.utils.data import Dataset
  File "/usr/local/lib/python3.8/dist-packages/torch/__init__.py", line 203, in <module>
    from torch._C import _initExtension
ImportError: cannot import name '_initExtension'

从报错中我发现问题所在,果然是程序寻找的路径出现了问题——sentence-transformer是安装在python3.6的torch环境下的,但程序是在python3.8的base环境中执行(虽然我明明在torch环境下运行的程序),这样肯定是找不到的。所以和上述解决方案一样,添加python3.6的环境变量。

export PYTHONPATH=/root/anaconda3/envs/torch/lib/python3.6/site-packages:$PYTHONPATH

再次执行就可以成功运行啦!

(torch) root@1c113923969c:/data/WWW# python /data/WWW/extra/TextEncoder/sentence_transformer.py
Downloading: 100%|#######################################################################################################################################################################################################| 1.18k/1.18k [00:00<00:00, 763kB/s]
Downloading: 100%|###########################################################################################################################################################################################################| 190/190 [00:00<00:00, 128kB/s]
Downloading: 100%|######################################################################################################################################################################################################| 10.6k/10.6k [00:00<00:00, 6.07MB/s]
Downloading: 100%|###########################################################################################################################################################################################################| 612/612 [00:00<00:00, 408kB/s]
Downloading: 100%|##########################################################################################################################################################################################################| 116/116 [00:00<00:00, 75.3kB/s]
Downloading: 100%|######################################################################################################################################################################################################| 39.3k/39.3k [00:00<00:00, 65.5kB/s]
Downloading: 100%|######################################################################################################################################################################################################| 90.9M/90.9M [00:02<00:00, 35.7MB/s]
Downloading: 100%|########################################################################################################################################################################################################| 53.0/53.0 [00:00<00:00, 40.7kB/s]
Downloading: 100%|##########################################################################################################################################################################################################| 112/112 [00:00<00:00, 92.8kB/s]
Downloading: 100%|#########################################################################################################################################################################################################| 466k/466k [00:04<00:00, 101kB/s]
Downloading: 100%|###########################################################################################################################################################################################################| 350/350 [00:00<00:00, 280kB/s]
Downloading: 100%|######################################################################################################################################################################################################| 13.2k/13.2k [00:00<00:00, 8.99MB/s]
Downloading: 100%|########################################################################################################################################################################################################| 232k/232k [00:03<00:00, 74.6kB/s]
Downloading: 100%|###########################################################################################################################################################################################################| 349/349 [00:00<00:00, 291kB/s]

The cat sits outside             The dog plays in the garden             Score: 0.2838
A man is playing guitar                  A woman watches TV              Score: -0.0327
The new movie is awesome                 The new movie is so great               Score: 0.8939
Sentence: This framework generates embeddings for each input sentence
Embedding: [-1.37173524e-02 -4.28515933e-02 -1.56286098e-02  1.40537601e-02

还有一种可能是 pip install 的时候并没有安装到指定的虚拟环境中,所以在安装过程中我们可以设置超参数来指定pip安装的虚拟环境,以安装opencv-python为例:

pip install --target=/root/anaconda3/envs/torch/lib/python3.6/site-packages  opencv-python==4.2.0.34  -i  https://pypi.doubanio.com/simple

其中env_name是目标虚拟环境名称,-i https://pypi.doubanio.com/simple是镜像。

到这里,安装就可以完成了,但还是友情提示一下:

--target一定不要写错!最保险的做法是写成绝对路径的形式

因为我发现当我使用相对路径的写法

pip install --target=~/anaconda3/envs/env_name/lib/python3.6/site-packages opencv-python==4.2.0.34  -i  https://pypi.doubanio.com/simple

还是会报错,而且没有找到安装的spellchecker

(torch) root@1c113923969c:~# pip install --target=~/anaconda3/envs/torch/lib/python3.6/site-packages pyspellchecker
Collecting pyspellchecker
  Using cached pyspellchecker-0.7.0-py3-none-any.whl (2.5 MB)
Installing collected packages: pyspellchecker
Successfully installed pyspellchecker-0.7.0
(torch) root@1c113923969c:~# python
Python 3.6.12 |Anaconda, Inc.| (default, Sep  8 2020, 23:10:56)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import spellchecker
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'spellchecker'
>>> exit()
(torch) root@1c113923969c:~# conda list spellchecker
# packages in environment at /root/anaconda3/envs/torch:
#
# Name                    Version                   Build  Channel

说明还是没有安装到正确的位置,之后我发现/root目录下多出一个以~为名的文件夹:

在这里插入图片描述

所以把--target写成相对路径是不可取的 ⬇ ❌

破案了✌

参考资料
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Meilinger_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值