Home Backend Development Python Tutorial Python脚本文件打包成可执行文件的方法

Python脚本文件打包成可执行文件的方法

Jun 10, 2016 pm 03:11 PM
python executable file Pack Script

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

一则: 不需要依赖Python编译器就可以运行软件
二则: 不想让自己的源码公布出去

常用的工具有: py2exe、cx_freeze等

【工具:py2exe】

安装py2exe
安装该工具很简单:
只需要从官方网站:http://www.py2exe.org/下载与版本对应的安装程序,点击下一步即可完成安装。
安装后,执行import py2exe,不报错则表示安装成功!

复制代码 代码如下:

>>> import py2exe 
>>> 

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

使用py2exe

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


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

复制代码 代码如下:

from distutils.core import setup 
import py2exe 
 
setup(windows=['Hello.py']) 

第三步: 运行命令: 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 

第四步: 双击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.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 -*- 
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",        # 名字信息 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

See all articles