Docker虚拟环境Jupyter中Blender Python模块bpy导入失败求助
问题描述
我按照官方文档生成了适配Python 3.10(Blender 3.6要求版本)的bpy wheel包,在Docker容器及其虚拟环境中安装成功:
终端运行正常情况
- Docker本地终端:
root@c8771baad61f:/# python3 Python 3.10.11 (main, Apr 5 2023, 14:15:10) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import bpy WARN (bgl): source/blender/python/generic/bgl.c:2654 BPyInit_bgl: 'bgl' imported without an OpenGL backend. Please update your add-ons to use the 'gpu' module. In Blender 4.0 'bgl' will be removed. >>> print(bpy.app.version_string) 3.6.0 Alpha
- Docker虚拟环境终端:
(venv) root@1277ea4e3a7b:/# python3 Python 3.10.11 (main, Apr 5 2023, 14:15:10) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import bpy WARN (bgl): source/blender/python/generic/bgl.c:2654 BPyInit_bgl: 'bgl' imported without an OpenGL backend. Please update your add-ons to use the 'gpu' module. In Blender 4.0 'bgl' will be removed. >>> print(bpy.app.version_string) 3.6.0 Alpha
Jupyter运行报错情况
在虚拟环境的Jupyter中运行import bpy时出现报错:
File "test_blender.py", line 1, in <module> import bpy ImportError: /opt/venv/lib/python3.10/site-packages/bpy/lib/libusd_ms.so: undefined symbol: _ZNSt15__exception_ptr13exception_ptr9_M_addrefEv
补充测试结果
在Jupyter中执行以下代码,第一行通过os.system调用命令行可正常运行,但第二行直接import bpy仍报错:
os.system("python -c \"import bpy; print(bpy.app.version_string)\"") import bpy
我怀疑这和GCC版本有关,但为什么终端中能正常运行?该如何解决?
解决方法
1. 确认Jupyter使用的Python解释器
Jupyter可能未加载虚拟环境的Python内核,导致依赖库链接版本不匹配。
- 先在Jupyter中运行以下代码检查当前Python路径:
import sys print(sys.executable)
若输出不是虚拟环境内的Python路径(如/opt/venv/bin/python3),按以下步骤配置对应内核:
# 激活虚拟环境 source /opt/venv/bin/activate # 安装ipykernel pip install ipykernel # 将虚拟环境注册为Jupyter内核 python -m ipykernel install --user --name=venv-bpy
之后在Jupyter中切换到venv-bpy内核运行代码。
2. 同步环境变量
终端运行时的LD_LIBRARY_PATH等环境变量可能与Jupyter环境不一致,导致bpy无法找到正确依赖。
- 在终端执行
echo $LD_LIBRARY_PATH,记录输出路径。 - 在Jupyter中先配置环境变量再导入bpy:
import os os.environ['LD_LIBRARY_PATH'] = "/记录的路径:$LD_LIBRARY_PATH" import bpy
或启动Jupyter前先设置环境变量:
source /opt/venv/bin/activate export LD_LIBRARY_PATH="/记录的路径:$LD_LIBRARY_PATH" jupyter notebook
3. 重新编译bpy时启用静态链接
若为GCC版本兼容性问题,编译bpy时添加-DUSE_STATIC_LIBS=ON选项,强制静态链接依赖库,避免动态版本冲突:
make -C ../blender-build install DISTRO=debian USE_STATIC_LIBS=ON
4. 指定正确的C++标准库
报错符号属于C++标准库,可能是Jupyter环境中libstdc++.so版本过低。可手动指定使用编译bpy时的标准库:
import ctypes ctypes.CDLL("/path/to/correct/libstdc++.so.6", mode=ctypes.RTLD_GLOBAL) import bpy
内容的提问来源于stack exchange,提问作者vwertuzy
相关产品推荐
相关产品推荐

