在 Conda 环境中,通过以下命令安装 paddlepaddle:

conda install paddlepaddle-gpu=2.4.2 cudatoolkit=11.6 -c conda-forge -c Paddle

安装好后,执行 Python 脚本import paddle会出现以下错误:

Python 3.9.16 (main, Mar  8 2023, 14:00:05)
[GCC 11.2.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import paddle
Error: Can not import paddle core while this file exists: /home/xxx/.conda/envs/paddle/lib/python3.9/site-packages/paddle/fluid/libpaddle.so
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/xxx/.conda/envs/paddle/lib/python3.9/site-packages/paddle/__init__.py", line 25, in <module>
    from .framework import monkey_patch_variable
  File "/home/xxx/.conda/envs/paddle/lib/python3.9/site-packages/paddle/framework/__init__.py", line 17, in <module>
    from . import random  # noqa: F401
  File "/home/xxx/.conda/envs/paddle/lib/python3.9/site-packages/paddle/framework/random.py", line 16, in <module>
    import paddle.fluid as fluid
  File "/home/xxx/.conda/envs/paddle/lib/python3.9/site-packages/paddle/fluid/__init__.py", line 36, in <module>
    from . import framework
  File "/home/xxx/.conda/envs/paddle/lib/python3.9/site-packages/paddle/fluid/framework.py", line 37, in <module>
    from . import core
  File "/home/xxx/.conda/envs/paddle/lib/python3.9/site-packages/paddle/fluid/core.py", line 338, in <module>
    raise e
  File "/home/xxx/.conda/envs/paddle/lib/python3.9/site-packages/paddle/fluid/core.py", line 274, in <module>
    from . import libpaddle
ImportError: libcudart.so.11.0: cannot open shared object file: No such file or directory

这个问题在 2.4.0 版本下就已经有人报了“Paddle 2.4.0 版本 fluid/core.py 文件 import libpaddle 出错”,但到现在 2.4.2 还未解决。

原因是libpaddle.soRPATH设置的不对。

$ patchelf --print-rpath /home/xxx/.conda/envs/paddle/lib/python3.9/site-packages/paddle/fluid/libpaddle.so
$ORIGIN/../libs/

根据Conda官方文档RPATH里应该有$ORIGIN/../lib/才对。直接给libpaddle.so添加上是不行的,因为RPATH的字符长度是在编译是就设定好了,除了重新编译没有其他工具提供直接修改的能力。

可以通过 Conda 提供的自定义环境变量功能来修改 LD_LIBRARY_PATH 来解决上述问题。

切换到需要修改的 Conda 环境,并执行以下命令:

cd $CONDA_PREFIX
mkdir -p ./etc/conda/activate.d
mkdir -p ./etc/conda/deactivate.d
touch ./etc/conda/activate.d/env_vars.sh
touch ./etc/conda/deactivate.d/env_vars.sh

编辑./etc/conda/activate.d/env_vars.sh内容为:

export CONDA_PREV_LD_LIBRARY_PATH=$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=$CONDA_PREFIX/lib/:$LD_LIBRARY_PATH

编辑./etc/conda/deactivate.d/env_vars.sh内容为:

export LD_LIBRARY_PATH=$CONDA_LD_LIBRARY_PATH
unset CONDA_LD_LIBRARY_PATH

通过上述设置后,再进 Conda 环境就不会再出上述错误。


  1. Paddle 2.4.0 版本 fluid/core.py 文件 import libpaddle 出错
  2. Using shared libraries
  3. Saving environment variables

2 对 “解决Conda下“ImportError: libcudart.so.11.0: cannot open shared object file: No such file or directory”错误”的想法;

    1. 建议你安装和我一样的版本的Paddle和cuda。
      如果还是不行,建议查看动态库依赖,看到底依赖哪个版本?路径指向哪里?

发表回复

您的电子邮箱地址不会被公开。