【问题标题】:Convert .py file into .pyd file将 .py 文件转换为 .pyd 文件
【发布时间】:2018-07-22 14:00:06
【问题描述】:

经过大量搜索无法找到合适的解决方案,我有我的 python 文件“Main.py”。我想要它的 .pyd 文件,即 Main.pyd。我已经尝试过使用 Cpython 的方式,即首先我将“Main.py”文件转换为“Main.c”,但随后无法将“Main.c”转换为“Main.pyd”,这是一种非常艰难的方式。我能有一种简单的方法可以将“Main.py”转换为“Main.pyd”吗?

【问题讨论】:

    标签: python-3.x dll cpython pyd


    【解决方案1】:

    我认为您只需要从您的脚本创建一个库。在您的sample_code.py 之外创建一个新的setup.py

    from distutils.core import setup
    from distutils.extension import Extension
    from Cython.Distutils import build_ext
    ext_modules = [
        Extension("sample_code",  ["sample_code.py"]),
    ]
    
    setup(
        name = 'My Program',
        cmdclass = {'build_ext': build_ext},
        ext_modules = ext_modules
    )
    

    然后使用python setup.py build_ext --inplace 生成您的库。 您可以找到更多信息here

    【讨论】:

    • 看来此代码需要预先安装 cython 包作为要求。反正有没有 cython 编译它?如果我的设备中没有绝对没有 C 编译器怎么办?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-26
    • 2021-10-03
    • 2020-11-23
    • 2014-04-11
    • 1970-01-01
    • 2023-03-18
    • 2017-05-22
    相关资源
    最近更新 更多