python-3.x VS代码:未找到模块错误:没有名为“sklearn”的模块

7y4bm7vi  于 2023-02-06  发布在  Python
关注(0)|答案(5)|浏览(490)

我正在VS代码中工作,以便在名为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'

我已经尝试了下面建议的所有可能的解决方案,但没有一个对我有效:

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

pcww981p

pcww981p1#

据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>
xqnpmsa8

xqnpmsa82#

你试过https://code.visualstudio.com/docs/python/environments
有同样的问题,并通过设置vscode使用我的conda环境解决了它。

lztngnrs

lztngnrs3#

我遇到过类似的问题,在应用了所有推荐的解决方案后,问题仍然存在,直到后来我发现我的VS-Code Python解释器指向了一个不同的虚拟环境。
我把它改成了工作虚拟环境,一切都运行得非常好。

ljo96ir5

ljo96ir54#

我也有同样的问题,但当我尝试这个命令的错误得到修复:
pip安装sklearn

k10s72fa

k10s72fa5#

单击VS代码中的终端并运行以下命令,以在VS代码中创建虚拟环境。
python -m venv path location of the working file\myvenv
在VS代码中,它将自动激活。
我附上了一张照片

相关问题