如何使用cython编译和链接多个python模块(或包)? [英] How to compile and link multiple python modules (or packages) using cython?

查看:938
本文介绍了如何使用cython编译和链接多个python模块(或包)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个python模块(组织成包),彼此依赖 Module3

  • Module4:导入Module3,模块2,模块1



  • 开发应用程序在Module4,我想使用 cython 生成Module4.so。
    如果我以朴素的方式继续,我得到一个扩展Module4.so,我可以导入 BUT 扩展依赖于Python1 Module2的python源代码 ,Module3。



    有没有方法编译,以便Module1,Module2,Module3 编译和链接到Module4?我想避免手动执行所有操作,例如首先编译Module1.so然后更改Module2中的import声明,以便导入Module1.so而不是Module1.py,然后将Module2编译为Module2.so等等....



    谢谢!

    解决方案

    编辑。前两个选项指的是Cython的具体代码,我错过的是,问题是关于纯python模块,所以选项3是解决方案。



    选项:



    1。
    请参阅如何在包中创建模块的层次结构:
    http:// wiki.cython.org/PackageHierarchy



    2。
    我更喜欢include语句:
    http://docs.cython.org/src/userguide/language_basics.html#the-include-statement
    我有很多.pyx文件,他们都包含在main.pyx中,它全部在一个命名空间。结果是一个大模块: http://code.google.com/ p / cefpython / source / browse / cefpython.pyx



    3。
    您可以通过添加多个扩展程序同时编译所有模块:

      setup b $ b cmdclass = {'build_ext':build_ext},
    ext_modules = [Extension(example,sourcefiles),Extension(example2,sourcefiles2),Extension(example3,sourcefiles3)]

    4。
    更高效的编译 - 请参见此处

     安装(
    name ='MyProject',
    ext_modules = cythonize([*。pyx]),


    I have several python modules (organized into packages), which depend on each other. e.g.

    • Module1
    • Module2: imports Module1
    • Module3
    • Module4: imports Module3, Module 2, Module 1

    Let's assume the relevant interface to develop applications is in Module4 and I want to generate a Module4.so using cython. If I proceed in the naive way, I get an extension Module4.so which I can import BUT the extension relies on the python source code of Module1, Module2, Module3.

    Is there a way to compile so that also Module1,Module2, Module3 are compiled and linked to Module4? I would like to avoid doing everything manually, e.g. first compile Module1.so then change import declaration in Module2, so as to import Module1.so rather than Module1.py, then compile Module2 into Module2.so and so on....

    Thanks!

    解决方案

    Edit. First two options refer to Cython's specific code, what I've missed is that the question is about pure python modules, so option 3 is the solution.

    There are a few options:

    1. See this "How To Create A Hierarchy Of Modules In A Package": http://wiki.cython.org/PackageHierarchy

    2. I prefer the "include" statement: http://docs.cython.org/src/userguide/language_basics.html#the-include-statement I have many .pyx files and they are all included in main.pyx, it's all in one namespace. The result is one big module: http://code.google.com/p/cefpython/source/browse/cefpython.pyx

    3. You can compile all your modules at once using setup by adding more than one "Extension":

    setup(
        cmdclass = {'build_ext': build_ext},
        ext_modules = [Extension("example", sourcefiles), Extension("example2", sourcefiles2), Extension("example3", sourcefiles3)]
    )
    

    4. A more efficent compilation - see here.

    setup (
        name = 'MyProject',
        ext_modules = cythonize(["*.pyx"]),
    )
    

    这篇关于如何使用cython编译和链接多个python模块(或包)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆