请选择 进入手机版 | 继续访问电脑版

技术控

    今日:35| 主题:61447
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Creating an executable file using Cython

[复制链接]
☆訫跳ルミ 发表于 2016-10-1 11:42:00
189 3
Disclaimer: I am quite new to Cython, if you find any part of this post is incorrect or there are better ways to do something, I would really appreciate your feedback. Please do feel free to leave your thoughts in the comments section :)  
  I know Cython is supposed to be used for building extensions, but I was wondering if we can by any chance compile a Python file into executable binary using Cython? I searched on Google and found this    StackOverflowquestion. There is a detailed answer on this question which is very helpful. I tried to follow the instructions and after (finding and ) fixing some paths, I managed to do it. I am going to write down my experience here in case someone else finds it useful as well.  
  Embedding the Python Interpreter

  Cython compiles the Python or the Cython files into C and then compiles the C code to create the extensions. Interestingly, Cython has a CLI switch    --embedwhic can generate a    mainfunction. This main function embeds the Python interpreter for us. So we can just compile the C file and get our single binary executable.  
  Getting Started

  First we need to have a Python (    .py) or Cython (    .pyx) file ready for compilation. Let’s start with a plain old “Hello World” example.  
  1. print("Hello World!")
复制代码
Let’s convert this Python file to a C source file with embedded Python interpreter.
  1. cython --embed -o hello_world.c hello_world.py
复制代码
It should generate a file named    hello_world.cin the current directory. We now compile it to an executable.  
  1. gcc -v -Os -I /Users/masnun/.pyenv/versions/3.5.1/include/python3.5m -L /usr/local/Frameworks/Python.framework/Versions/3.5/lib  -o test test.c  -lpython3.5  -lpthread -lm -lutil -ldl
复制代码
Please note you must have the Python source code and dynamic libraries in order to successfully compile it. I am on OSX and I use PyEnv. So I passed the appropriate paths and it compiled fine.
  Now I have an executable file, which I can run:
  1. $ ./hello_world
  2. Hello World!
复制代码
Dynamic Linking

  In this case, the executable we produce is dynamically linked to our specified Python version. So this may not be fully portable (the libraries will need to be available on target machines). But this should work fine if we compile against common versions (for example the default version of Python or a version easily obtainable via the package manager).
  Including Other Modules

  Up untill now, I haven’t found any easy ways to include other 3rd party pure python modules (ie.    requests) directly compiled into the binary. However, if I want to split my codes into multiple files, I can create other    .pyxfiles and use the    includestatement with those.  
  For example, here’s    hello.pyx:  
  1. cdef struct Person:
  2.     char *name
  3.     int age
  4. cdef say():
  5.     cdef Person masnun = Person(name="masnun", age=20)
  6.     print("Hello {}, you are {} years old!".format(masnun.name.decode('utf8'), masnun.age))
复制代码
And here’s my main file -    test.pyx-  
  1. include "hello.pyx"
  2. say()
复制代码
Now if I compile    test.pyxjust like above example, it will also include the code in    hello.pyxand I can call the    sayfunction as if it was in    test.pyxitself.  
  However, shared libraries like PyQt would have no issues - we can compile them as is. So basically we can take any PyQt code example and compile it with Cython - it should work fine!
刘洪根 发表于 2016-10-8 12:38:51
你长的拖慢网速,你长的太耗内存,大哥,把你脸上的分辨率调低点好吗?国际脸孔世界通用。
回复 支持 反对

使用道具 举报

廖鑫 发表于 2016-11-1 02:11:28
有钱,任性!
回复 支持 反对

使用道具 举报

1123213148 发表于 2016-11-16 01:41:17
抢位子,坐等更新!
回复 支持 反对

使用道具 举报

我要投稿

推荐阅读


回页顶回复上一篇下一篇回列表
手机版/c.CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表