这种类似的问题遇到好多次了……今天使用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=~/anaconda3/envs/env_name/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
是镜像
时好时坏的,奇了怪了……
(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 "" , 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