技术控

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

[其他] Creating an executable file using Cython

[复制链接]
☆訫跳ルミ 发表于 2016-10-1 11:42:00
150 3

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
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!
友荐云推荐




上一篇:Robust Landmark-Based Audio Fingerprinting (2012)
下一篇:ISC updates critical DoS bug in BIND DNS software
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

刘洪根 发表于 2016-10-8 12:38:51
你长的拖慢网速,你长的太耗内存,大哥,把你脸上的分辨率调低点好吗?国际脸孔世界通用。
回复 支持 反对

使用道具 举报

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

使用道具 举报

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

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表