安装的 scikit-learn 无法正常工作

Installed scikit-learn doesn't work properly

当我 运行 以下代码时出现此错误:

from sklearn.decomposition import LatentDirichletAllocation

ImportError: cannot import name '__check_build' from partially initialized module 'sklearn' (most likely due to a circular import).

当我检查 pip freeze scikit-learn 是否已安装时。 另外,我尝试卸载并重新安装 sklearn,现在我得到了一个不同的错误:

ERROR: Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory: 'C:\Users\<user>\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\sklearn\datasets\tests\data\openml\292\api-v1-json-data-list-data_name-australian-limit-2-data_version-1-status-deactivated.json.gz'

根据https://scikit-learn.org/stable/install.html

It can happen that pip fails to install packages when reaching the default path size limit of Windows if Python is installed in a nested location such as the AppData folder structure under the user home directory, for instance:

C:\Users\username>C:\Users\username\AppData\Local\Microsoft\WindowsApps\python.exe -m pip install scikit-learn
Collecting scikit-learn
...
Installing collected packages: scikit-learn
ERROR: Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory: 'C:\Users\username\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\sklearn\datasets\tests\data\openml\292\api-v1-json-data-list-data_name-australian-limit-2-data_version-1-status-deactivated.json.gz

In this case it is possible to lift that limit in the Windows registry by using the regedit tool:

  1. 在 Windows 开始菜单中键入“regedit”以启动 regedit。

  2. 转到 Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem 键。

  3. 编辑该键的 LongPathsEnabled 属性 值并将其设置为 1。

  4. 重新安装 scikit-learn(忽略之前损坏的安装):

pip install --exists-action=i scikit-learn