FreeCAD集成Python控制台导入sklearn失败问题及解决
导入scikit-learn时ModuleNotFoundError的问题与解决
环境信息
Windows: 10.0.19044 系统默认Python版本: 3.7.3 已安装Scikit-learn版本: 1.0.2 Numpy: 1.21.6 Scipy: 1.7.3 FreeCAD版本: 0.20.1
问题场景
需要使用sklearn.neighbors模块,在FreeCAD 0.20.1的集成Python控制台中,先通过sys.path.append()添加scikit-learn的安装路径,再执行import sklearn,但无论是导入sklearn还是sklearn.neighbors,都会触发相同报错。
报错信息
>>> import sklearn Traceback (most recent call last): File "C:\\RoboDK\\Python37\\Lib\\site-packages\\sklearn\\__check_build\\__init__.py", line 48, in <module> from ._check_build import check_build # noqa File "C:\\Program Files\\FreeCAD 0.20\\bin\\Lib\\site-packages\\shiboken2\\files.dir\\shibokensupport\\__feature__.py", line 142, in _import return original_import(name, *args, **kwargs) ModuleNotFoundError: No module named 'sklearn.__check_build._check_build' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<input>", line 1, in <module> File "C:\\Program Files\\FreeCAD 0.20\\bin\\Lib\\site-packages\\shiboken2\\files.dir\\shibokensupport\\__feature__.py", line 142, in _import return original_import(name, *args, **kwargs) File "C:\\RoboDK\\Python37\\Lib\\site-packages\\sklearn\\__init__.py", line 81, in <module> from . import __check_build # noqa: F401 File "C:\\Program Files\\FreeCAD 0.20\\bin\\Lib\\site-packages\\shiboken2\\files.dir\\shibokensupport\\__feature__.py", line 142, in _import return original_import(name, *args, **kwargs) File "C:\\RoboDK\\Python37\\Lib\\site-packages\\sklearn\\__check_build\\__init__.py", line 50, in <module> raise_build_error(e) File "C:\\RoboDK\\Python37\\Lib\\site-packages\\sklearn\\__check_build\\__init__.py", line 31, in raise_build_error raise ImportError( ImportError: No module named 'sklearn.__check_build._check_build' ___________________________________________________________________________ Contents of C:\\RoboDK\\Python37\\Lib\\site-packages\\sklearn\\__check_build: setup.py _check_build.cp37-win_amd64.pyd__init__.py __pycache__ ___________________________________________________________________________ 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.
已尝试的无效操作
- 多次以管理员权限卸载、重装scikit-learn
- 尝试安装不同旧版本的scikit-learn
- 执行官方推荐的检查命令,结果均显示scikit-learn 1.0.2版本已正常安装:
python -m pip show scikit-learn # 查看scikit-learn版本及安装路径 python -m pip freeze # 查看当前环境下所有已安装包 python -c "import sklearn; sklearn.show_versions()"
解决方法
FreeCAD自带Python 3.8环境,此前为系统默认Python 3.7安装的scikit-learn无法适配该环境。执行以下命令为Python 3.8环境安装对应版本的scikit-learn:
py -3.8 -m pip install scikit-learn==1.2.2
安装完成后,导入scikit-learn的问题即可解决。
内容的提问来源于stack exchange,提问作者wonderpet
相关产品推荐
相关产品推荐

