突然无法调用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
相关推荐
AI小云6 小时前
【数据操作与可视化】Pandas数据处理-其他操作
python·pandas
大佬,救命!!!6 小时前
更换适配python版本直接进行机器学习深度学习等相关环境配置(非仿真环境)
人工智能·python·深度学习·机器学习·学习笔记·详细配置
无心水6 小时前
【Python实战进阶】4、Python字典与集合深度解析
开发语言·人工智能·python·python字典·python集合·python实战进阶·python工业化实战进阶
上班职业摸鱼人6 小时前
python文件中导入另外一个模块这个模块
python
永远是夏天7 小时前
Python面向对象编程(OOP)全教程:从入门到实战(附案例)
python
动感小麦兜7 小时前
服务器搭建
linux·服务器·python
Pocker_Spades_A7 小时前
在家写的代码,办公室接着改?Jupyter通过cpolar实现远程访问这么玩
ide·python·jupyter
m5655bj8 小时前
使用 Python 高效复制 Excel 行、列、单元格
开发语言·python·excel
龙言龙论8 小时前
身份证信息批量处理系统:从入门到实战(附exe工具+核心源码)
数据库·python
yLDeveloper8 小时前
致深度学习小白:一文理解拟合问题与经典解决方案
机器学习·dive into deep learning