将Windows上的python脚本交叉编译成linux可执行文件

时间:2012-04-29 12:44:38

标签: python windows ubuntu cross-compiling

我在Windows上使用Python创建了一个程序。你如何把它变成Linux可执行文件?具体来说就是Linux Ubuntu 9.10。

3 个答案:

答案 0 :(得分:3)

Tendou,

由于我通常在Linux机器上开发,当我需要一个Windows版本时,我经常使用cx_freeze来制作Windows二进制文件,但我也用它来制作Linux二进制文件并且它工作得很好(它是多平台的,py2exe不是)。就像Eli上面说的那样,它并没有真正编译,只是将解释器,字节编译代码和所需的库捆绑成一个可执行文件,但你会得到你想要的东西(在没有安装python的机器上运行)。

在Linux机器上安装cx_freeze之后,你可以尝试一些简单的东西,比如这个虚拟代码,我保存为 test.py

import time

for i in range(10):
  print i
  time.sleep(1)

然后运行 cxfreeze 来制作二进制文件。输出将如下所示:

$ cxfreeze test.py 
copying /usr/lib/python2.6/site-packages/cx_Freeze/bases/Console -> /home/claudio/dist/test
copying /usr/lib/libpython2.6.so.1.0 -> /home/claudio/dist/libpython2.6.so.1.0
writing zip file /home/claudio/dist/test

  Name                      File
  ----                      ----
m StringIO                  
m UserDict                  
m __builtin__               
m __main__                  test.py
m _abcoll                   
m _codecs                   
m _codecs_cn                /usr/lib/python2.6/lib-dynload/_codecs_cn.so
m _codecs_hk                /usr/lib/python2.6/lib-dynload/_codecs_hk.so
m _codecs_iso2022           /usr/lib/python2.6/lib-dynload/_codecs_iso2022.so
m _codecs_jp                /usr/lib/python2.6/lib-dynload/_codecs_jp.so
m _codecs_kr                /usr/lib/python2.6/lib-dynload/_codecs_kr.so
m _codecs_tw                /usr/lib/python2.6/lib-dynload/_codecs_tw.so
m _functools                /usr/lib/python2.6/lib-dynload/_functools.so
m _locale                   /usr/lib/python2.6/lib-dynload/_locale.so
m _multibytecodec           /usr/lib/python2.6/lib-dynload/_multibytecodec.so
m _sre                      
m _strptime                 /usr/lib/python2.6/_strptime.py
m _struct                   /usr/lib/python2.6/lib-dynload/_struct.so
m _warnings                 
m abc                       
m array                     /usr/lib/python2.6/lib-dynload/array.so
m base64                    
m binascii                  /usr/lib/python2.6/lib-dynload/binascii.so
m bz2                       /usr/lib/python2.6/lib-dynload/bz2.so
m cStringIO                 /usr/lib/python2.6/lib-dynload/cStringIO.so
m calendar                  /usr/lib/python2.6/calendar.py
m codecs                    
m copy                      
m copy_reg                  
m cx_Freeze__init__         /usr/lib/python2.6/site-packages/cx_Freeze/initscripts/Console.py
m datetime                  /usr/lib/python2.6/lib-dynload/datetime.so
m dummy_thread              /usr/lib/python2.6/dummy_thread.py
P encodings                 
m encodings.aliases         
m encodings.ascii           
m encodings.base64_codec    
m encodings.big5            
m encodings.big5hkscs       
m encodings.bz2_codec       
m encodings.charmap         
m encodings.cp037           
m encodings.cp1006          
m encodings.cp1026          
m encodings.cp1140          
m encodings.cp1250          
m encodings.cp1251          
m encodings.cp1252          
m encodings.cp1253          
m encodings.cp1254          
m encodings.cp1255          
m encodings.cp1256          
m encodings.cp1257          
m encodings.cp1258          
m encodings.cp424           
m encodings.cp437           
m encodings.cp500           
m encodings.cp737           
m encodings.cp775           
m encodings.cp850           
m encodings.cp852           
m encodings.cp855           
m encodings.cp856           
m encodings.cp857           
m encodings.cp860           
m encodings.cp861           
m encodings.cp862           
m encodings.cp863           
m encodings.cp864           
m encodings.cp865           
m encodings.cp866           
m encodings.cp869           
m encodings.cp874           
m encodings.cp875           
m encodings.cp932           
m encodings.cp949           
m encodings.cp950           
m encodings.euc_jis_2004    
m encodings.euc_jisx0213    
m encodings.euc_jp          
m encodings.euc_kr          
m encodings.gb18030         
m encodings.gb2312          
m encodings.gbk             
m encodings.hex_codec       
m encodings.hp_roman8       
m encodings.hz              
m encodings.idna            
m encodings.iso2022_jp      
m encodings.iso2022_jp_1    
m encodings.iso2022_jp_2    
m encodings.iso2022_jp_2004 
m encodings.iso2022_jp_3    
m encodings.iso2022_jp_ext  
m encodings.iso2022_kr      
m encodings.iso8859_1       
m encodings.iso8859_10      
m encodings.iso8859_11      
m encodings.iso8859_13      
m encodings.iso8859_14      
m encodings.iso8859_15      
m encodings.iso8859_16      
m encodings.iso8859_2       
m encodings.iso8859_3       
m encodings.iso8859_4       
m encodings.iso8859_5       
m encodings.iso8859_6       
m encodings.iso8859_7       
m encodings.iso8859_8       
m encodings.iso8859_9       
m encodings.johab           
m encodings.koi8_r          
m encodings.koi8_u          
m encodings.latin_1         
m encodings.mac_arabic      
m encodings.mac_centeuro    
m encodings.mac_croatian    
m encodings.mac_cyrillic    
m encodings.mac_farsi       
m encodings.mac_greek       
m encodings.mac_iceland     
m encodings.mac_latin2      
m encodings.mac_roman       
m encodings.mac_romanian    
m encodings.mac_turkish     
m encodings.mbcs            
m encodings.palmos          
m encodings.ptcp154         
m encodings.punycode        
m encodings.quopri_codec    
m encodings.raw_unicode_escape 
m encodings.rot_13          
m encodings.shift_jis       
m encodings.shift_jis_2004  
m encodings.shift_jisx0213  
m encodings.string_escape   
m encodings.tis_620         
m encodings.undefined       
m encodings.unicode_escape  
m encodings.unicode_internal 
m encodings.utf_16          
m encodings.utf_16_be       
m encodings.utf_16_le       
m encodings.utf_32          
m encodings.utf_32_be       
m encodings.utf_32_le       
m encodings.utf_7           
m encodings.utf_8           
m encodings.utf_8_sig       
m encodings.uu_codec        
m encodings.zlib_codec      
m errno                     
m functools                 /usr/lib/python2.6/functools.py
m genericpath               
m gettext                   /usr/lib/python2.6/gettext.py
m imp                       
m itertools                 /usr/lib/python2.6/lib-dynload/itertools.so
m linecache                 
m locale                    /usr/lib/python2.6/locale.py
m operator                  /usr/lib/python2.6/lib-dynload/operator.so
m optparse                  /usr/lib/python2.6/optparse.py
m os                        
m posix                     
m posixpath                 
m pwd                       
m quopri                    
m re                        /usr/lib/python2.6/re.py
m repr                      
m sre_compile               /usr/lib/python2.6/sre_compile.py
m sre_constants             /usr/lib/python2.6/sre_constants.py
m sre_parse                 /usr/lib/python2.6/sre_parse.py
m stat                      
m string                    /usr/lib/python2.6/string.py
m stringprep                
m strop                     /usr/lib/python2.6/lib-dynload/strop.so
m struct                    
m sys                       
m textwrap                  /usr/lib/python2.6/textwrap.py
m thread                    
m time                      /usr/lib/python2.6/lib-dynload/time.so
m token                     /usr/lib/python2.6/token.py
m tokenize                  /usr/lib/python2.6/tokenize.py
m traceback                 
m types                     
m unicodedata               /usr/lib/python2.6/lib-dynload/unicodedata.so
m warnings                  
m zipimport                 
m zlib                      /usr/lib/python2.6/lib-dynload/zlib.so

copying /usr/lib/python2.6/lib-dynload/_codecs_cn.so -> /home/claudio/dist/_codecs_cn.so
copying /usr/lib/python2.6/lib-dynload/_codecs_hk.so -> /home/claudio/dist/_codecs_hk.so
copying /usr/lib/python2.6/lib-dynload/_codecs_iso2022.so -> /home/claudio/dist/_codecs_iso2022.so
copying /usr/lib/python2.6/lib-dynload/_codecs_jp.so -> /home/claudio/dist/_codecs_jp.so
copying /usr/lib/python2.6/lib-dynload/_codecs_kr.so -> /home/claudio/dist/_codecs_kr.so
copying /usr/lib/python2.6/lib-dynload/_codecs_tw.so -> /home/claudio/dist/_codecs_tw.so
copying /usr/lib/python2.6/lib-dynload/_functools.so -> /home/claudio/dist/_functools.so
copying /usr/lib/python2.6/lib-dynload/_locale.so -> /home/claudio/dist/_locale.so
copying /usr/lib/python2.6/lib-dynload/_multibytecodec.so -> /home/claudio/dist/_multibytecodec.so
copying /usr/lib/python2.6/lib-dynload/_struct.so -> /home/claudio/dist/_struct.so
copying /usr/lib/python2.6/lib-dynload/array.so -> /home/claudio/dist/array.so
copying /usr/lib/python2.6/lib-dynload/binascii.so -> /home/claudio/dist/binascii.so
copying /usr/lib/python2.6/lib-dynload/bz2.so -> /home/claudio/dist/bz2.so
copying /usr/lib/python2.6/lib-dynload/cStringIO.so -> /home/claudio/dist/cStringIO.so
copying /usr/lib/python2.6/lib-dynload/datetime.so -> /home/claudio/dist/datetime.so
copying /usr/lib/python2.6/lib-dynload/itertools.so -> /home/claudio/dist/itertools.so
copying /usr/lib/python2.6/lib-dynload/operator.so -> /home/claudio/dist/operator.so
copying /usr/lib/python2.6/lib-dynload/strop.so -> /home/claudio/dist/strop.so
copying /usr/lib/python2.6/lib-dynload/time.so -> /home/claudio/dist/time.so
copying /usr/lib/python2.6/lib-dynload/unicodedata.so -> /home/claudio/dist/unicodedata.so
copying /usr/lib/python2.6/lib-dynload/zlib.so -> /home/claudio/dist/zlib.so

请注意,对于较大的项目,您可能会收到“Missing modules”错误。如果您确定已安装它们,则可以使用 - include-modules 开关手动将它们指向cx_freeze。有关完整帮助,请参阅 cxfreeze --help

您还可以创建一个 setup.py 文件,使“cxfreezing”类似于我们通常安装python包的方式,而不是直接从命令行执行。本文甚至对 wxPython 项目执行此操作,该项目可能与您的类似: A cx_Freeze Tutorial – Build a Binary Series!

答案 1 :(得分:1)

可以帮助您做到这一点的一些项目是py2execx_freezepyinstaller

请注意,他们所做的并不是经典意义上的“编译”。相反,他们可以将Python解释器与程序所需的所有库捆绑到一个可执行文件中。

答案 2 :(得分:0)

我没有在Linux上部署应用程序的经验 - 但是在为apt-get打包软件时,是否无法添加依赖项?我安装了一直引入其他库的软件包。好像你可以为wx做这个。