Synology DS220+迁移用户后scikit-learn 0.24.1运行报错求助
问题:Synology DS220+迁移用户名后,虚拟环境中scikit-learn脚本运行报错
在Synology DS220+上将用户名从“admin”迁移至“username”后,原通过SSH每日运行的Python程序出现异常。已通过以下步骤在虚拟环境中安装scikit-learn 0.24.1:
sudo python3 -m pip install virtualenv python3 -m virtualenv venv source venv/bin/activate pip install scikit-learn==0.24.1
执行pip show scikit-learn确认安装信息:
pip show scikit-learn >Name: scikit-learn Version: 0.24.1 Summary: A set of python modules for machine learning and data mining Home-page: http://scikit-learn.org Author: Author-email: License: new BSD Location: /volume1/homes/username/venv/lib/python3.8/site-packages Requires: joblib, numpy, scipy, threadpoolctl Required-by:
但运行指定脚本时出现如下错误:
username@synology:~$ venv/bin/python3 /volume1/homes/username/Drive/stock/portfolio/price_updater.py Traceback (most recent call last): File "/volume1/homes/username/venv/lib/python3.8/site-packages/sklearn/__check_build/__init__.py", line 44, in <module> from ._check_build import check_build # noqa ImportError: dlopen: cannot load any more object with static TLS During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/volume1/homes/username/Drive/stock/portfolio/price_updater.py", line 6, in <module> from new.portbot import PortfolioBot File "/volume1/homes/username/Drive/stock/new/portbot.py", line 25, in <module> from NewStock import Stock File "/volume1/homes/username/Drive/stock/NewStock.py", line 17, in <module> from new.buyingprice import BuyingPrice File "/volume1/homes/username/Drive/stock/new/buyingprice.py", line 13, in <module> from new.pricetarget import PriceTarget File "/volume1/homes/username/Drive/stock/new/pricetarget.py", line 19, in <module> from sklearn.linear_model import LinearRegression File "/volume1/homes/username/venv/lib/python3.8/site-packages/sklearn/__init__.py", line 81, in <module> from . import __check_build # noqa: F401 File "/volume1/homes/username/venv/lib/python3.8/site-packages/sklearn/__check_build/__init__.py", line 46, in <module> raise_build_error(e) File "/volume1/homes/username/venv/lib/python3.8/site-packages/sklearn/__check_build/__init__.py", line 31, in raise_build_error raise ImportError("""%s ImportError: dlopen: cannot load any more object with static TLS ___________________________________________________________________________ Contents of /volume1/homes/username/venv/lib/python3.8/site-packages/sklearn/__check_build: _check_build.cpython-38-x86_64-linux-gnu.sosetup.py __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.
奇怪的是,Python交互环境中可正常导入sklearn:
username@Synology:~$ venv/bin/python3 Python 3.8.6 (default, Jan 4 2021, 16:11:49) [GCC 4.9.3 20150311 (prerelease)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import sklearn >>>
解决方法
1. 调整脚本导入顺序
将sklearn相关的导入语句移至脚本最顶部,避免其他库先占用静态TLS槽位。例如,在price_updater.py的第一行添加:
from sklearn.linear_model import LinearRegression
2. 重新编译scikit-learn并禁用静态TLS
激活虚拟环境后,使用以下命令重新安装scikit-learn,编译时禁用静态TLS:
CFLAGS="-fno-static-tls" pip install --force-reinstall --no-binary :all: scikit-learn==0.24.1
3. 升级scikit-learn版本
较新版本的scikit-learn已修复部分静态TLS问题,若脚本兼容,可直接升级:
pip install --upgrade scikit-learn
4. 调整系统环境变量
运行脚本前设置环境变量,预加载pthread库:
export LD_PRELOAD=/lib64/libpthread.so.0 venv/bin/python3 /volume1/homes/username/Drive/stock/portfolio/price_updater.py
也可将该环境变量添加到虚拟环境的venv/bin/activate脚本末尾,每次激活时自动生效。
内容的提问来源于stack exchange,提问作者mrkgoh
相关产品推荐
相关产品推荐

