5

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

4

3 回答 3

4

天斗,

由于我通常在 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

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

您还可以创建一个setup.py文件,使“cxfreezing”类似于我们通常安装 python 包的方式,而不是直接从命令行进行。这篇文章甚至对一个wxPython项目也这样做了,它可能与你的项目相似: A cx_Freeze 教程 – 构建二进制系列!

于 2012-05-04T20:54:43.730 回答
2

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

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

于 2012-04-29T12:57:35.220 回答
0

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

于 2012-05-04T21:21:48.473 回答