Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross compiling a python script on windows into linux executable

I've created a program using Python on Windows. How do you turn it into Linux executable? To be specific Linux Ubuntu 9.10.

like image 890
絢瀬絵里 Avatar asked Apr 29 '12 12:04

絢瀬絵里


People also ask

Can you cross-compile Python?

Pure-Python libraries usually just work, but many popular libraries rely on compiled C code, which can be challenging to build. This package is a tool for cross-compiling extension modules.


2 Answers

Tendou,

Since I usually develop on a Linux machine, when I need a windows version I often use cx_freeze to make windows binaries, but I also used it to make Linux binaries and it works pretty well (it is multiplatform, something py2exe is not). Like Eli said above, it doesn't really compile, but just bundles the interpreter, byte compiled code and needed libraries into an executable, but you'll get what you want (running on a machine without python pre-installed).

After having installed cx_freeze on a Linux machine, you can try it out with something simple, like this dummy code, which i saved as test.py:

import time

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

Then run cxfreeze to make the binary. The output will look like this:

$ 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

Note that with bigger projects you might get "Missing modules" errors. If you are sure they are installed, you can manually point them to cx_freeze using the --include-modules switch. See cxfreeze --help for the full help.

You can also make a setup.py file which makes "cxfreezing" resemble the way we usually install python packages, instead of doing it directly from the command line. This article even does that for a wxPython project, which might be similar to yours: A cx_Freeze Tutorial – Build a Binary Series!

like image 150
Claudio Avatar answered Oct 12 '22 11:10

Claudio


Some projects that can help you do that are py2exe, cx_freeze and pyinstaller.

Do note that what they all do is not "compiling" in the classic sense. Rather, they can bundle the Python interpreter with all the libraries required for your program into a single executable.

like image 2
Eli Bendersky Avatar answered Oct 12 '22 12:10

Eli Bendersky