V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
KADGang
V2EX  ›  Python

求助:有没有办法可以在运行时找到 Pybind11 所需的共享库?

  •  
  •   KADGang · 139 天前 · 635 次点击
    这是一个创建于 139 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我目前在使用 Pybind11 为我的 C++库创建 Python 扩展,但是现在我遇到了一个问题:我编写 setup.py 并将扩展打包成 wheel 包,但是在运行时它会报错说找不到对应的动态库:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ImportError: libtest_linux64.so: cannot open shared object file: No such file or directory
    

    我的一部分 setup.py 代码如下(一部分文件名做了改动):

    ext_modules = [
        Pybind11Extension(
            "test",
            sources=[
                "src/test.cpp"
            ],
            libraries=["lib/libtest_linux64.so"],
            library_dirs=["lib"]
            include_dirs=[
                "include"
            ],
            runtime_library_dirs=[sysconfig.get_config_var("LIBDIR")],
            language='c++'
        ),
    ]
    
    
    
    setup(
        name='myname',
        version=version_code,
        author='author',
        author_email='[email protected]',
        long_description='',
        ext_modules=ext_modules,
        data_files=["lib/libtest_linux64.so"],
        cmdclass={'build_ext': build_ext},
        zip_safe=False,
        python_requires=">=3.6",
    )
    

    这个 wheel 包安装到我打包的机器上后运行正常,但是如果我把这个包改到另一台机器上就会报上面的错误。但是后来我发现它之所以能在机器上运行是因为动态库文件都会被创建在 conda 对应环境的 lib 下,而这个路径可能是已经在环境变量里所以才能被找到。

    我认为问题出在runtime_library_dirs=[sysconfig.get_config_var("LIBDIR")],因为这导致打包时扩展运行时的搜索路径被固定在打包机的 python 环境的 LIBDIR 上,所以在另一个不同环境的机器上不适用。有什么办法可以解决这个问题吗?

    附: 我也想在 MacOS 上做类似的打包。但是经过我的搜索后发现runtime_library_dirs这个成员似乎在 macos 上不适用,好像是要用什么 @rpath 之类的还要用一个什么改名工具。求问各位 V 友老哥有没有什么办法可以让这个项目在经过bdist_wheel后打出来的包能够被运行在不同的机器上?

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2495 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 14:27 · PVG 22:27 · LAX 07:27 · JFK 10:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.