将Python脚本文件包装成可执行文件,其目的有二:

一则: 不需要依赖Python编译器就可以运行软件

二则: 不想让自己的源码公布出去

常用的工具有: py2exe、cx_freeze等

【工具:py2exe】

安装py2exe

安装该工具很简单:

只需要从官方网站:http://www.py2exe.org/下载与版本对应的安装程序,点击下一步即可完成安装。

安装后,执行import py2exe,不报错则表示安装成功!

>>>importpy2exe

>>>

>>> import py2exe

>>>

NOTE: 目前该工具只支持到Python2.7, 对于Python3而言,必须借助另外一个工具:cx_freeze

使用py2exe

第一步: 准备源代码,假如名为:Hello.py

第二步:准备编译脚本,假如名为:setup.py

fromdistutils.coreimportsetup

importpy2exe

setup(windows=['Hello.py'])

from distutils.core import setup

import py2exe

setup(windows=['Hello.py'])

第三步: 运行命令: setup.py py2exe

D:\temp>setup.py py2exe

D:\temp>setup.py py2exe

运行之后,会在我当前运行的目录下(D:\temp)默认生成dict目录,里面的文件如下:

默认情况下,py2exe在目录dist下创建以下这些必须的文件:

1、一个或多个exe文件。如本例为: Hello.exe

2、python##.dll。 如本例中: Python27.dll

3、.pyd文件,它们是已编译的扩展名,它们是exe文件所需要的;加上其它的.dll文件,这些.dll是.pyd所需要的。

4、一个library.zip文件,它包含了已编译的纯的python模块如.pyc或.pyo

默认情况下,py2exe在目录dist下创建以下这些必须的文件:

1、一个或多个exe文件。如本例为: Hello.exe

2、python##.dll。 如本例中: Python27.dll

3、.pyd文件,它们是已编译的扩展名,它们是exe文件所需要的;加上其它的.dll文件,这些.dll是.pyd所需要的。

4、一个library.zip文件,它包含了已编译的纯的python模块如.pyc或.pyo

第四步: 双击Hello.exe可执行文件,跟源代码运行后同样的结果:

其他

1:执行setup.py --help获取帮助信息

Global options:

--verbose (-v) run verbosely (default)

--quiet (-q) run quietly (turns verbosity off)

--dry-run (-n) don't actually do anything

--help (-h) show detailed help message

--no-user-cfg ignore pydistutils.cfginyour home directory

Optionsfor'py2exe'command:

--optimize (-O) optimization level: -O1for"python -O", -O2for

"python -OO",and-O0 to disable [default: -O0]

--dist-dir (-d) directory to put final built distributionsin(default

isdist)

--excludes (-e) comma-separated list of modules to exclude

--dll-excludes comma-separated list of DLLs to exclude

--ignores comma-separated list of modules to ignoreifthey are

notfound

--includes (-i) comma-separated list of modules to include

--packages (-p) comma-separated list of packages to include

--compressed (-c) create a compressed zipfile

--xref (-x) createandshow a module cross reference

--bundle-files (-b) bundle dllsinthe zipfileorthe exe. Valid levels

are1,2,or3(default)

--skip-archive donotplace Python bytecode filesinan archive, put

them directlyinthe file system

--ascii (-a) donotautomatically include encodingsandcodecs

--custom-boot-script Python file that will be run when setting up the

runtime environment

usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]

or: setup.py --help [cmd1 cmd2 ...]

or: setup.py --help-commands

or: setup.py cmd --help

Global options:

--verbose (-v) run verbosely (default)

--quiet (-q) run quietly (turns verbosity off)

--dry-run (-n) don't actually do anything

--help (-h) show detailed help message

--no-user-cfg ignore pydistutils.cfg in your home directory

Options for 'py2exe' command:

--optimize (-O) optimization level: -O1 for "python -O", -O2 for

"python -OO", and -O0 to disable [default: -O0]

--dist-dir (-d) directory to put final built distributions in (default

is dist)

--excludes (-e) comma-separated list of modules to exclude

--dll-excludes comma-separated list of DLLs to exclude

--ignores comma-separated list of modules to ignore if they are

not found

--includes (-i) comma-separated list of modules to include

--packages (-p) comma-separated list of packages to include

--compressed (-c) create a compressed zipfile

--xref (-x) create and show a module cross reference

--bundle-files (-b) bundle dlls in the zipfile or the exe. Valid levels

are 1, 2, or 3 (default)

--skip-archive do not place Python bytecode files in an archive, put

them directly in the file system

--ascii (-a) do not automatically include encodings and codecs

--custom-boot-script Python file that will be run when setting up the

runtime environment

usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]

or: setup.py --help [cmd1 cmd2 ...]

or: setup.py --help-commands

or: setup.py cmd --help

2: 一个详细的编译脚本

# -*- coding: cp936 -*-

fromdistutils.coreimportsetup

importpy2exe

includes = ["encodings","encodings.*"]

options = {"py2exe":

{"compressed":1,# 压缩

"optimize":2,# 优化级别

"ascii":1,#

"includes":includes,# 编码方式

"bundle_files":1# 所有文件打包成一个zipfile或exe文件,有效级别1,2,3

}}

setup(

options=options,# 是否需要可选项,默认为None

zipfile=None,# 是否需要压缩像,默认为None

console=[{"script":"HelloCmd.py","icon_resources": [(1,"pc.ico")]}],# 针对CMD控制端口

windows=[{"script":"HelloWin.py","icon_resources": [(1,"pc.ico")]}],# 针对GUI图形窗口

data_files=[("magic",["App_x86.exe",]),],

version ="v1.01",# 版本信息

description ="py2exe testing",# 描述信息

name ="Hello, Py2exe",# 名字信息

)

# -*- coding: cp936 -*-

from distutils.core import setup

import py2exe

includes = ["encodings", "encodings.*"]

options = {"py2exe":

{"compressed": 1, # 压缩

"optimize": 2, # 优化级别

"ascii": 1, #

"includes":includes, # 编码方式

"bundle_files": 1 # 所有文件打包成一个zipfile或exe文件,有效级别1,2,3

}}

setup(

options=options, # 是否需要可选项,默认为None

zipfile=None, # 是否需要压缩像,默认为None

console=[{"script": "HelloCmd.py", "icon_resources": [(1, "pc.ico")]}], # 针对CMD控制端口

windows=[{"script": "HelloWin.py", "icon_resources": [(1, "pc.ico")]}], # 针对GUI图形窗口

data_files=[("magic",["App_x86.exe",]),],

version = "v1.01", # 版本信息

description = "py2exe testing",# 描述信息

name = "Hello, Py2exe", # 名字信息

)

详情,请参考官方文档:

linux运行python脚本_将Python脚本文件包装成可执行文件相关推荐

  1. Python把对应格式的csv文件转换成字典类型存储脚本的方法_python_脚本之家

    该脚本是为了结合之前的编写的脚本,来实现数据的比对模块,实现数据的自动化!由于数据格式是定死的,该代码只做参考,有什么问题可以私信我! CSV的数据格式截图如下: readDataToDic.py源代 ...

  2. 第一章 第一节:Python基础_认识Python

    Python基础入门(全套保姆级教程) 第一章 第一节:Python基础_认识Python 1. 什么是编程 通俗易懂,编程就是用代码编写程序,编写程序有很多种办法,像c语言,javaPython语言 ...

  3. Python 文件打包成可执行文件

    import randomwhile True:#系统给出随机数字sys_num = random.randint(1,10)print(sys_num)while True:print(" ...

  4. shell python运维脚本_【Python运维】最简单的Python运维脚本

    背景 最近在Windows 10上使用Linux子系统,发现它有一个非常坑爹的特点:Linux子系统是没有开机关机状态的,每次进入Bash shell就自动载入,退出后Linux子系统的所有进程都会被 ...

  5. python sql脚本_使用Python SQL脚本进行数据采样

    python sql脚本 介绍 (Introduction) The Python programming language is object oriented, easy to use and, ...

  6. python是不是脚本_关于Python是不是脚本语言的探讨

    很多人认为Python是单纯的脚本语言,认为它是一门简单的语言.其实,脚本语言并不是所谓的简单,而是简洁.Python可以使得一些复杂的编程任务变得简单而不是简单的编程语言.所以要是给Python一个 ...

  7. python扫雷脚本_利用 Python 实现 自动扫雷 小脚本

    原标题:利用 Python 实现 自动扫雷 小脚本 自动扫雷一般分为两种,一种是读取内存数据,而另一种是通过分析图片获得数据,并通过模拟鼠标操作,这里我用的是第二种方式.一.准备工作1.扫雷游戏 我是 ...

  8. python嵌入式脚本_基于Python的嵌入式脚本研究

    基于Python的嵌入式脚本研究 王国强;张贝克 [期刊名称]<计算机应用与软件> [年(卷),期]2010(027)003 [摘要]嵌入脚本语言到应用程序当中在用户自动化和个性化定制方面 ...

  9. python 运行程序代码_一些python程序

    <从问题到程序:用Python学编程和计算>--1.2 Python语言简介 本节书摘来自华章计算机<从问题到程序:用Python学编程和计算>一书中的第1章,第1.2节,作者 ...

最新文章

  1. 背包模型dp2之二维费用背包
  2. [HNOI2008] Cards
  3. JavaScript代码检验工具——JS Lint工具安装指南
  4. Linux下Nginx编译安装后的开机自启动设置
  5. 压缩感知(I) A Compressed Sense of Compressive Sensing (I)
  6. (PCB)进程控制块
  7. cloudstack centOS安装(二)
  8. Python批量提取PDF文件中的文本
  9. 剑指offer——替换字符串
  10. postgresql-定时备份,压缩备份
  11. 一次http请求中的信息
  12. Atitit  如何让精灵控件运动
  13. java类型的对象可以存储属性_重识JVM(一)-类与对象在JVM中是如何存储的
  14. java飞机大战游戏
  15. 新华书店牵手阿里云 要用云计算打造“智慧悦读”
  16. Mysql性能调优之max_allowed_packet
  17. [BZOJ4152][AMPPZ2014]The Captain题解
  18. 找到数组里面缺失的数(C++(绝妙方法))
  19. effective c++ 学习笔记之 Shifting from c to c++
  20. [程序设计]前端Web页面使用原生JavaScript实现校验身份证号码在算法层面是否合法

热门文章

  1. python博客下载本地文件_Linux CentOS6安装Git、Node.js及Hexo静态博客安装和使用方法...
  2. NOTA-WL12,68Ga-NOTA-WL12一种基于肽的正电子发射断层扫描 (PET) 显像剂
  3. WebLogic BEA-101020问题
  4. java基础之 IO流
  5. 前端 HTML(1)
  6. 浅谈 TS 标称类型介绍及社区实现
  7. Centos7搭建私服
  8. D1. Coffee and Coursework (Easy version) and D2. Coffee and Coursework (Hard Version)
  9. markdown 希腊字母
  10. Android第三方绑定QQ无法拉取授权页面