突然无法调用scikit-learn、xgboost

遇到的错误包含:

python 复制代码
  File "<string>", line 3, in <module>
  File "/usr/anaconda3/envs/py37/lib/python3.7/site-packages/sklearn/__init__.py", line 81, in <module>
    from . import __check_build  # noqa: F401
  File "/usr/anaconda3/envs/py37/lib/python3.7/site-packages/sklearn/__check_build/__init__.py", line 50, in <module>
    raise_build_error(e)
  File "/usr/anaconda3/envs/py37/lib/python3.7/site-packages/sklearn/__check_build/__init__.py", line 43, in raise_build_error
    % (e, local_dir, "".join(dir_content).strip(), msg)
ImportError: dlopen: cannot load any more object with static TLS
___________________________________________________________________________
Contents of /usr/anaconda3/envs/py37/lib/python3.7/site-packages/sklearn/__check_build:
__init__.py               setup.py                  _check_build.cpython-37m-x86_64-linux-gnu.so
__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.
python 复制代码
  File "<string>", line 5, in <module>
  File "/usr/anaconda3/envs/py37/lib/python3.7/site-packages/xgboost/__init__.py", line 6, in <module>
    from .core import (
  File "/usr/anaconda3/envs/py37/lib/python3.7/site-packages/xgboost/core.py", line 231, in <module>
    _LIB = _load_lib()
  File "/usr/anaconda3/envs/py37/lib/python3.7/site-packages/xgboost/core.py", line 197, in _load_lib
    """
xgboost.core.XGBoostError: 
XGBoost Library (libxgboost.so) could not be loaded.
Likely causes:
  * OpenMP runtime is not installed
    - vcomp140.dll or libgomp-1.dll for Windows
    - libomp.dylib for Mac OSX
    - libgomp.so for Linux and other UNIX-like OSes
    Mac OSX users: Run `brew install libomp` to install OpenMP runtime.

  * You are running 32-bit Python on a 64-bit OS

Error message(s): ['dlopen: cannot load any more object with static TLS']

解决办法:

在脚本最开头加如下代码

python 复制代码
# 添加在最顶部
import ctypes
import os

# 强制预加载关键库(先于任何其他导入)
# ctypes.CDLL('libgomp.so.1', mode=ctypes.RTLD_GLOBAL)
# 强制预加载 OpenMP 库
try:
    ctypes.CDLL('libgomp.so.1', mode=ctypes.RTLD_GLOBAL)
except OSError:
    # 如果上述失败,尝试指定具体路径
    ctypes.CDLL('/usr/anaconda3/envs/py37/lib/libgomp.so.1', mode=ctypes.RTLD_GLOBAL)

ctypes.CDLL('libpthread.so.0', mode=ctypes.RTLD_GLOBAL)

# 设置OpenMP环境变量
os.environ["OMP_NUM_THREADS"] = "1"
os.environ["MKL_NUM_THREADS"] = "1"
os.environ["KMP_INIT_AT_FORK"] = "FALSE"

# # 恢复导入路径(如有必要)
# import sys
# sys.path.insert(0, '/usr/anaconda3/envs/py37/lib/python3.7/site-packages')

# 以下是你的原始代码...
import sklearn  # 这行应该在预加载后
import xgboost
相关推荐
星火开发设计5 小时前
Python数列表完全指南:从基础到实战
开发语言·python·学习·list·编程·知识·期末考试
星浩AI5 小时前
LCEL:打造可观测、可扩展、可部署的 LangChain 应用
人工智能·后端·python
这儿有一堆花5 小时前
Python 虚拟环境的配置与管理指南
开发语言·python
Generalzy6 小时前
深度觉醒 — Deep Agents(三座大山 — Agent 的核心挑战)
python
YukiMori236 小时前
基于Paddle微调ERNIE的中文情感分析实战教程
深度学习·机器学习
彼岸花开了吗6 小时前
构建AI智能体:七十八、参数的艺术:如何在有限算力下实现高质量的AI诗歌创作
人工智能·python·llm
小途软件6 小时前
基于深度学习的人脸检测算法研究
java·人工智能·pytorch·python·深度学习·语言模型
墨&白.6 小时前
机器学习速成笔记week9:决策树ID3、C4.5和CART的底层逻辑
笔记·决策树·机器学习
guoketg6 小时前
Vision Transformer(ViT)的讲解和面试题目讲解
人工智能·python·深度学习·vit
小oo呆6 小时前
【学习心得】Python的Pydantic(简介)
前端·javascript·python