在linux下交叉编译python,在windows上交叉编译一个python脚本到linux可执行文件

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

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

您还可以制作一个setup.py文件,它使得“cxfreezing”类似于我们通常安装python软件包的方式,而不是直接从命令行执行。这篇文章甚至会替一个wxPython的项目,这可能是与你相似: A cx_Freeze Tutorial – Build a Binary Series!

你可能感兴趣的:(在linux下交叉编译python,在windows上交叉编译一个python脚本到linux可执行文件)