繁体   English   中英

VS 代码:ModuleNotFoundError:没有名为“sklearn”的模块

[英]VS Code: ModuleNotFoundError: No module named 'sklearn'

我正在使用 VS Code 在名为myenvconda环境中运行一个 Python 脚本,其中已经安装了sklearn 但是,当我导入它并运行脚本时,出现以下错误:

Traceback (most recent call last):
  File "d:\ML\Project\src\train.py", line 5, in <module>
    from sklearn.linear_models import LinearRegression
ModuleNotFoundError: No module named 'sklearn'

我已经尝试了以下建议的所有可能的解决方案,但对我没有任何帮助:

有人可以建议一种不同的方法来解决这个问题吗?

根据 pypi:

使用pip install scikit-learn而不是pip install sklearn

根据我的经验,这有效:

C:\Users\gfernandez>pip install sklearn
Collecting sklearn
Using cached sklearn-0.0.post1-py3-none-any.whl
Installing collected packages: sklearn
Successfully installed sklearn-0.0.post1
C:\Users\gfernandez>pip install scikit-learn
Requirement already satisfied: scikit-learn in c:\users\gfernandez\appdata\local\programs\python\python310\lib\site-packages (1.1.3)
Requirement already satisfied: scipy>=1.3.2 in c:\users\gfernandez\appdata\local\programs\python\python310\lib\site-packages (from scikit-learn) (1.9.3)
Requirement already satisfied: numpy>=1.17.3 in c:\users\gfernandez\appdata\local\programs\python\python310\lib\site-packages (from scikit-learn) (1.23.4)
Requirement already satisfied: threadpoolctl>=2.0.0 in c:\users\gfernandez\appdata\local\programs\python\python310\lib\site-packages (from scikit-learn) (3.1.0)
Requirement already satisfied: joblib>=1.0.0 in c:\users\gfernandez\appdata\local\programs\python\python310\lib\site-packages (from scikit-learn) (1.2.0)
C:\Users\gfernandez>

你试过https://code.visualstudio.com/docs/python/environments

有同样的问题并通过将 vscode 设置为使用我的 conda 环境来解决它。

我也有同样的问题,但是当我尝试这个命令时,错误得到了修复:

pip 安装sklearn

我有一个类似的问题,在应用所有推荐的解决方案后,问题一直存在,直到我后来发现我的 VS-Code Python 解释器指向不同的虚拟环境。

我将其更改为工作虚拟环境,一切都运行良好。

在 VS Code 中点击终端,运行以下命令,在 VS Code 中创建虚拟环境。

python -m venv path location of the working file\myvenv

在 VS Code 中它会自动激活。

我附上了一张图片

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM