无法通过Anaconda安装包/创建环境,出现KeyError及SSL模块错误
Conda安装scikit-learn及创建环境报错解决
我使用Miniconda 3、Spyder 5.5.5和Python 3.10.11,尝试通过Spyder或终端安装scikit-learn、创建新环境时均遭遇报错,错误信息如下:
KeyError('active_prefix_name') Traceback (most recent call last): File "/opt/miniconda3/lib/python3.12/site-packages/conda/exception_handler.py", line 18, in __call__ return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/opt/miniconda3/lib/python3.12/site-packages/conda/cli/main.py", line 52, in main_subshell from .conda_argparse import do_call, generate_parser, generate_pre_parser File "/opt/miniconda3/lib/python3.12/site-packages/conda/cli/conda_argparse.py", line 51, in <module> from .main_create import configure_parser as configure_parser_create File "/opt/miniconda3/lib/python3.12/site-packages/conda/cli/main_create.py", line 16, in <module> from ..notices import notices File "/opt/miniconda3/lib/python3.12/site-packages/conda/notices/__init__.py", line 3, in <module> from .core import notices # noqa: F401 ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/miniconda3/lib/python3.12/site-packages/conda/notices/core.py", line 15, in <module> from . import cache, fetch, views File "/opt/miniconda3/lib/python3.12/site-packages/conda/notices/fetch.py", line 11, in <module> import requests File "/opt/miniconda3/lib/python3.12/site-packages/requests/__init__.py", line 164, in <module> from .api import delete, get, head, options, patch, post, put, request File "/opt/miniconda3/lib/python3.12/site-packages/requests/api.py", line 11, in <module> from . import sessions File "/opt/miniconda3/lib/python3.12/site-packages/requests/sessions.py", line 15, in <module> from .adapters import HTTPAdapter File "/opt/miniconda3/lib/python3.12/site-packages/requests/adapters.py", line 76, in <module> _preloaded_ssl_context = create_urllib3_context() ^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/miniconda3/lib/python3.12/site-packages/urllib3/util/ssl_.py", line 252, in create_urllib3_context raise TypeError("Can't create an SSLContext object without an ssl module") TypeError: Can't create an SSLContext object without an ssl module During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/opt/miniconda3/lib/python3.12/site-packages/conda/exception_handler.py", line 165, in print_unexpected_error_report get_main_info_str(error_report["conda_info"]) File "/opt/miniconda3/lib/python3.12/site-packages/conda/cli/main_info.py", line 399, in get_main_info_str return "\n".join(("", *(f"{key:>23} : {value}" for key, value in builder()), "")) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/miniconda3/lib/python3.12/site-packages/conda/cli/main_info.py", line 399, in <genexpr> return "\n".join(("", *(f"{key:>23} : {value}" for key, value in builder()), "")) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/miniconda3/lib/python3.12/site-packages/conda/cli/main_info.py", line 359, in builder if info_dict["active_prefix_name"]: ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^ KeyError: 'active_prefix_name' # >>>>>>>>>>>>>>>>>>>>>> ERROR REPORT <<<<<<<<<<<<<<<<<<<< Traceback (most recent call last): File "/opt/miniconda3/lib/python3.12/site-packages/conda/exception_handler.py", line 18, in __call__ return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/opt/miniconda3/lib/python3.12/site-packages/conda/cli/main.py", line 52, in main_subshell from .conda_argparse import do_call, generate_parser, generate_pre_parser File "/opt/miniconda3/lib/python3.12/site-packages/conda/cli/conda_argparse.py", line 51, in <module> from .main_create import configure_parser as configure_parser_create File "/opt/miniconda3/lib/python3.12/site-packages/conda/cli/main_create.py", line 16, in <module> from ..notices import notices File "/opt/miniconda3/lib/python3.12/site-packages/conda/notices/__init__.py", line 3, in <module> from .core import notices # noqa: F401 ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/miniconda3/lib/python3.12/site-packages/conda/notices/core.py", line 15, in <module> from . import cache, fetch, views File "/opt/miniconda3/lib/python3.12/site-packages/conda/notices/fetch.py", line 11, in <module> import requests File "/opt/miniconda3/lib/python3.12/site-packages/requests/__init__.py", line 164, in <module> from .api import delete, get, head, options, patch, post, put, request File "/opt/miniconda3/lib/python3.12/site-packages/requests/api.py", line 11, in <module> from . import sessions File "/opt/miniconda3/lib/python3.12/site-packages/requests/sessions.py", line 15, in <module> from .adapters import HTTPAdapter File "/opt/miniconda3/lib/python3.12/site-packages/requests/adapters.py", line 76, in <module> _preloaded_ssl_context = create_urllib3_context() ^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/miniconda3/lib/python3.12/site-packages/urllib3/util/ssl_.py", line 252, in create_urllib3_context raise TypeError("Can't create an SSLContext object without an ssl module") TypeError: Can't create an SSLContext object without an ssl module `$ /opt/miniconda3/bin/conda install scikit-learn` environment variables: conda info could not be constructed. KeyError('active_prefix_name') An unexpected error has occurred. Conda has prepared the above report. If you suspect this error is being caused by a malfunctioning plugin, consider using the --no-plugins option to turn off plugins. Example: conda --no-plugins install <package> Alternatively, you can set the CONDA_NO_PLUGINS environment variable on the command line to run the command without plugins enabled. Example: CONDA_NO_PLUGINS=true conda install <package> If submitted, this report will be used by core maintainers to improve future releases of conda. Would you like conda to send this report to the core maintainers? [y/N]: Timeout reached. No report sent.
我已尝试创建新环境,但仍出现相同错误。
解决步骤
1. 禁用Conda插件尝试安装
根据报错提示,先排除插件干扰:
conda --no-plugins install scikit-learn
或者通过环境变量临时禁用插件:
CONDA_NO_PLUGINS=true conda install scikit-learn
2. 修复SSL模块缺失问题
核心错误是SSL模块缺失,按系统类型处理:
- Debian/Ubuntu系:安装SSL依赖库
sudo apt-get install libssl-dev - CentOS/RHEL系:安装SSL依赖库
sudo yum install openssl-devel - 重新安装对应Python3.10版本的Miniconda,确保安装过程中SSL模块正常集成(安装前可备份现有环境)。
3. 重置Conda环境
如果Conda本身出现损坏,执行重置命令:
conda init --all conda update conda --no-plugins
4. 绕过Conda用Pip安装
若上述方法无效,激活目标环境后使用Pip安装:
conda activate your_env_name # 替换为你的环境名称,或直接用base环境 pip install scikit-learn
内容的提问来源于stack exchange,提问作者Ava R
相关产品推荐
相关产品推荐

