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


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

    from spellchecker import SpellChecker
    ModuleNotFoundError: No module named 'spellchecker'
    
    • 1
    • 2

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

    export PYTHONPATH=path:$PYTHONPATH
    
    • 1

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

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

    这个问题就迎刃而解了。然后又想到前段时间安装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'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

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

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

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

    (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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    还有一种可能是 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
    
    • 1

    其中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
    
    • 1

    还是会报错,而且没有找到安装的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 "", 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

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

    在这里插入图片描述

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

    破案了✌

    参考资料
  • 相关阅读:
    [kernel] 带着问题看源码 —— 进程 ID 是如何分配的
    Spark On Yarn基本原理及部署
    神经网络预测指标是什么,神经网络怎么预测数据
    电厂数据可视化三维大屏展示平台加强企业安全防范
    一位末流211新大二同学的暑期总结
    AGV移动机器人无人叉车控制器设计
    uniapp常见兼容性问题
    SpringBoot3整合SpringDoc实现在线接口文档
    集合框架----源码解读HashSet篇
    双向链表(Double Linked List)
  • 原文地址:https://blog.csdn.net/qq_36332660/article/details/127984518