使用 pip3 install 后出现 ModuleNotFoundError

ModuleNotFoundError after using pip3 install

提问人:Ethan Cha 提问时间:11/17/2023 更新时间:11/18/2023 访问量:25

问:

最近,我一直在 Visual Studio Code 中学习数据科学,当我尝试使用它时,我一直在安装的所有模块都不断给出 ModuleNotFoundError。

我尝试将它们安装在 visual studio code 终端以及 mac 内部的终端中。这是我使用的命令python3 -m pip3 install scikit-learn

^ 我使用了各种可能的配置,例如使用 pip 而不是 pip3,甚至尝试下载 sklearn,因为我认为这是我必须得到的单独东西。

我还查看了其他堆栈溢出问题。

最近,我正在尝试使用包含sklearn的scikit-learn模块

这是我代码的导入部分:

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression #finds correlation between data and a trend line
from sklearn import metrics
import pandas as pd
import math

这是我收到的错误:"/Users/ethancha/Downloads/Data Science club/scikit.py" Traceback (most recent call last): File "/Users/ethancha/Downloads/Data Science club/scikit.py", line 7, in <module> from sklearn.model_selection import train_test_split ModuleNotFoundError: No module named 'sklearn'

从另一个堆栈溢出问题中,我尝试在代码顶部添加以下内容:

import sys

sys.path.insert(0, r"/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages")
sys.executable

这只是给出了一个新错误(我根据论坛复制了它存储位置的文件路径)。

错误:

Traceback (most recent call last):
  File "/Users/ethancha/Downloads/Data Science club/scikit.py", line 7, in <module>
    from sklearn.model_selection import train_test_split
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/sklearn/__init__.py", line 79, in <module>
    from . import (
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/sklearn/__check_build/__init__.py", line 47, in <module>
    raise_build_error(e)
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/sklearn/__check_build/__init__.py", line 31, in raise_build_error
    raise ImportError("""%s
ImportError: No module named 'sklearn.__check_build._check_build'
___________________________________________________________________________
Contents of /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/sklearn/__check_build:
__init__.py               __pycache__               _check_build.cpython-312-darwin.so
___________________________________________________________________________
It seems that scikit-learn has not been built correctly.

If you have installed scikit-learn from source, please do not forget
to build the package before using it: run `python setup.py install` or
`make` in the source directory.

If you have used an installer, please check that it is suited for your
Python version, your operating system and your platform.

如何在没有这些问题的情况下使用这些模块?

python-3.x scikit-learn pip 模块notfounderror

评论

0赞 compilation-error 11/17/2023
您需要检查哪个解释器 VSCode 正在使用?是否有正在使用的虚拟环境?您可以通过单击 VSCode 右下角的版本来找到它。或者,尝试从命令行运行python3 <filename>.py

答:

0赞 Ravi Kishan 11/18/2023 #1

尝试在 python 虚拟环境上工作

您需要创建一个 Python 虚拟环境并激活它

python -m venv venv

您必须一个名为 的文件夹。现在运行此命令venv

./venv/Scripts/activate

现在安装 SKLEARN

python3 -m pip3 install scikit-learn

还可以在 Visual Studio Code 中选择虚拟环境。 只需按 + + 并搜索并选择本地环境并运行 and shell 或运行 python 文件。ctrlshiftpSelect Interpreteripythonjupyter-notebook