python py生成exe_Python插件 pyinstaller打包.py文件生成exe

news/2024/7/7 15:55:20 标签: python py生成exe

安装pyinstaller

pyinstaller支持python2和python3

命令行安装:pip

install pyinstaller

217/

Note: windows下转换要先安装pywin32:pip

install pywin32[pywin32的安装]

皮皮blog

pyinstaller的使用

pi@PIPI

/e/mine/python_workspace/nlp(dev)

$pyinstaller -FE:/mine/python_workspace/NLP/TargetOpinion/TargetOpinionMain.py

在命令行当前路径/e/mine/python_workspace/nlp中会生成

dist目录(目录里面有可执行文件)

build目录(生成exe的中间文件)

spec文件(要转换文件的同一目录下,用于生成exe文件,可以修改来自定义生成exe的属性)

指定dist生成目录路径(而不是命令行当前目录)

pyinstaller -F

E:/mine/python_workspace/test0/testMain.py

--distpath=E:/mine/python_workspace/test0/dist

压缩生成的exe文件

用UPX去压缩,压缩后所生成的exe文件的大小,会小得多

--upxI mentioned that this is a great option, and itis, but it's really slow, especially as your source file getsbigger.It's a great option for your finalcompile before distributing, but you might save a lot of time ifyou turn it off until then.

pyinstaller参数中添加upx路径

pyinstaller -FE:/mine/python_workspace/NLP/TargetOpinion/TargetOpinionMain.py--upx-dir

upx391w

直接进入界面程序

pyinstaller生成的exe文件是从命令行开始执行的,如果之前的程序是界面程序(如pyqt界面开始执行的),则要改成从界面执行,这样就不会看到命令行输出了(如bug错误输出、print输出等)

pyinstaller-FwE:/mine/python_workspace/NLP/TargetOpinion/TargetOpinionMain.py

当然,如果是在调试期间,建议不要加w参数,这样就可以从命令行看到bug提示了。

附录:

pyinstaller参数

Options

-h,--helpshow this help message and exit

-v,--versionShow program version info and exit.

--distpathDIRWhere to put the bundled app (default: ./dist)

--workpathWORKPATH

Where to put all the temporary work files, .log, .pyz and etc.

(default: ./build)

-y,--noconfirm

Replace output directory (default: SPECPATH/dist/SPECNAME) without

asking for confirmation

--upx-dirUPX_DIR

Path to UPX utility (default: search the execution path)

-a,--asciiDo not include unicode encoding support (default: included if

available)

--cleanClean PyInstaller cache and remove temporary files before

building.

--log-levelLEVEL

Amount of detail in build-time console messages. LEVEL may be one

of DEBUG, INFO, WARN, ERROR, CRITICAL (default: INFO).

to generate

-D,--onedirCreate a one-folder bundle containing an executable (default)

-F,--onefileCreate a one-file bundled executable.

--specpathDIRFolder to store the generated spec file (default: current

directory)

-nNAME,--nameNAME

Name to assign to the bundled app and spec file (default: first

script's basename)

to bundle, where to search

-pDIR,--pathsDIR

A path to search for imports (like using PYTHONPATH). Multiple

paths are allowed, separated by ':', or use this option multiple

times

--hidden-importMODULENAME,--hiddenimportMODULENAME

Name an import not visible in the code of the script(s). This

option can be used multiple times.

--additional-hooks-dirHOOKSPATH

An additional path to search for hooks. This option can be used

multiple times.

--runtime-hookRUNTIME_HOOKS

Path to a custom runtime hook file. A runtime hook is code that is

bundled with the executable and is executed before any other code

or module to set up special features of the runtime environment.

This option can be used multiple times.

--exclude-moduleEXCLUDES

Optional module or package (his Python names, not path names) that

will be ignored (as though it was not found). This option can be

used multiple times.

--keyKEYThe key used to encrypt Python bytecode.

to generate

-d,--debugTell the bootloader to issue progress messages while initializing

and starting the bundled app. Used to diagnose problems with

missing imports.

-s,--stripApply a symbol-table strip to the executable and shared libs (not

recommended for Windows)

--noupxDo not use UPX even if it is available (works differently between

Windows and *nix)

and Mac OS X specific options

-c,--console,--nowindowed

Open a console window for standard i/o (default)

-w,--windowed,--noconsole

Windows and Mac OS X: do not provide a console window for standard

i/o. On Mac OS X this also triggers building an OS X .app bundle.

This option is ignored in *NIX systems.

-i,--icon

FILE.ico: apply that icon to a Windows executable. FILE.exe,ID,

extract the icon with ID from an exe. FILE.icns: apply the icon to

the .app bundle on Mac OS X

specific options

--version-fileFILE

add a version resource from FILE to the exe

-m,--manifest

add manifest FILE or XML to the exe

-rRESOURCE,--resourceRESOURCE

Add or update a resource to a Windows executable. The RESOURCE is

one to four items, FILE[,TYPE[,NAME[,LANGUAGE]]]. FILE can be a

data file or an exe/dll. For data files, at least TYPE and NAME

must be specified. LANGUAGE defaults to 0 or may be specified as

wildcard * to update all resources of the given TYPE and NAME. For

exe/dll files, all resources from FILE will be added/updated to the

final executable if TYPE, NAME and LANGUAGE are omitted or

specified as wildcard *.This option can be used multiple

times.

--uac-adminUsing this option creates a Manifest which will request elevation

upon application restart.

--uac-uiaccessUsing this option allows an elevated application to work with

Remote Desktop.

Side-by-side Assembly searching options (advanced)

--win-private-assemblies

Any Shared Assemblies bundled into the application will be changed

into Private Assemblies. This means the exact versions of these

assemblies will always be used, and any newer versions installed on

user machines at the system level will be ignored.

--win-no-prefer-redirects

While searching for Shared or Private Assemblies to bundle into the

application, PyInstaller will prefer not to follow policies that

redirect to newer versions, and will try to bundle the exact

versions of the assembly.

OS X specific options

--osx-bundle-identifierBUNDLE_IDENTIFIER

Mac OS X .app bundle identifier is used as the default unique

program name for code signing purposes. The usual form is a

hierarchical name in reverse DNS notation. For example:

com.mycompany.department.appname (default: first script's

basename)

the Command

Because of its numerous options, a fullpyinstallercommandcan become very long. You will run the same command again and againas you develop your script. You can put the command in a shellscript or batch file, using line continuations to make it readable.For example, in Linux:

pyinstaller --noconfirm --log-level=WARN \

--onefile --nowindow \

--hidden-import=secret1 \

--hidden-import=secret2 \

--upx-dir=/usr/local/share/ \

myscript.spec

Or in Windows, use the little-known BAT file line continuation:

pyinstaller --noconfirm --log-level=WARN ^

--onefile --nowindow ^

--hidden-import=secret1 ^

--hidden-import=secret2 ^

--icon-file=..\MLNMFLCN.ICO ^

myscript.spec

参数(翻译)

Note:--onefile前面是两个-。

-F    制作独立的可执行程序

-D    制作出的档案存放在同一个文件夹下(默认值)

-K    包含TCL/TK(对于使用了TK的,最好加上这个选项,否则在未安装TK的电脑上无法运行)

-w制作窗口程序

-c    制作命令行程序(默认)

-X    制作使用UPX压缩过的可执行程序(推荐使用这个选项,需要下载UPX包,解压后upx.exe放在Python(非PyInstaller)安装目录下,下载upx308w.zip)

-o DIR  指定输出SPEC文件路径(这也决定了最后输出的exe文件路径)

--icon=[ICO文件路径] 指定程序图标

-v [指定文件] 指定程序版本信息

-n [指定程序名] 指定程序名称

单独生成一个exe文件(-F参数)后,exe文件是怎么运行的:

How the One-File Program Works

The bootloader is the heart of the one-file bundle also. Whenstarted it creates a temporary folder in the appropriatetemp-folder location for this OS. The folder is named _MEIxxxxxx,where xxxxxx is a random number.

The one executable file contains an embedded archive of all thePython modules used by your script, as well as compressed copies ofany non-Python support files (e.g. .so files). The bootloaderuncompresses the support files and writes copies into the thetemporary folder. This can take a little time. That is why aone-file app is a little slower to start than a one-folderapp.

After creating the temporary folder, the bootloader proceedsexactly as for the one-folder bundle, in the context of thetemporary folder. When the bundled code terminates, the bootloaderdeletes the temporary folder.

What happens during execution of bootloader:

First process: bootloader starts.

If one-file mode, extract bundled filestotemppath_MEIxxxxxx

Set/unset various environment variables, e.g. override

LD_LIBRARY_PATH on Linux or LIBPATH on AIX; unset DYLD_LIBRARY_PATH

on OSX.

Set up to handle signals for both processes.

Run the child process.

Wait for the child process to finish.

If one-file mode, deletetemppath_MEIxxxxxx.注意,exe文件运行完成后,temp目录下的临时文件_mei***就会被删除。

Second process: bootloader itself started as a child process.

On Windows set theactivation

context.

Load the Python dynamic library. The name of the dynamic library is

embedded in the executable file.

Initialize Python interpreter: set sys.path, sys.prefix,

sys.executable.

Run python code.

Running Python code requires several steps:

Run the Python initialization code which prepares everything forrunning the user's main script. The initialization code can useonly the Python built-in modules because the general importmechanism is not yet available. It sets up the Python importmechanism to load modules only from archives embedded in theexecutable. It also adds the attributesfrozenand_MEIPASStothesysbuilt-inmodule.

Execute any run-time hooks: first those specified by the user, then

any standard ones.

Install python "egg" files. When a module is part of a zip file(.egg), it has been bundled into the./eggsdirectory.Installing means appending .egg file namestosys.path.Python automatically detects whether an iteminsys.pathisa zip file or a directory.

Run the main script.

guide to using PyInstaller


http://www.niftyadmin.cn/n/1860732.html

相关文章

真正的IT女是什么样子的?

真正的IT女是什么样子的?相信大家在回复这个问题的时候都会围绕着IT女的兴趣,爱情,喜好来作为一个女程序员的体现吧,但我个人认为还是应该从女程序独有的属性来回答这个问题! 比如女程序员在工作中的状态,学…

用 JAVA 开发游戏连连看(之四)添加更多的功能

之四)添加更多的功能 计分功能 大体上我们的程序已经可以跑了起来,可惜,就这么玩玩也太没有意思了,总得有个计分的吧。虽然我们不知道别人是怎么计分的,可是,程序是我们自己动手写的,我的地…

JAVA程序员遇到职业问题是跑路?还是留守?

初级JAVA程序员最担心在公司里遇到原本其他程序员开发的项目,他们“跑路”以后的工作就由新程序员完成。而新员工也不懂内部的逻辑,酱紫让程序员很难处理后续的问题?摆在他们面试的可能就两个词,离职! 坚持&#xff01…

线程之同步的两种条件总结

同步的两种表现形式: 1.同步代码块 synchronized(对象){ 需要同步的代码 } 2.同步函数: 使用的锁是this public synchronized void show(){ } 同步的作用:避免线程的安全隐患 单例 懒汉式 …

用 JAVA 开发游戏连连看(之五)完善用户界面

(之五)完善用户界面 让界面更动起来 整个程序的界面总算是出来了,可惜不太漂亮,这种界面,别说别人,就连自己也不愿意多看几眼,因此,做一些适当的美化工作还是非常有必要的。 想…

程序员是网管吗?

编程的叫程序员 ,修电脑的叫网管,怎么你们就不懂呢?程序猿等于修理工?网管?盗QQ?Photoshop?硬盘文件恢复?装系统? 那对于这种吃力不讨好,并且自己也不擅长的修…

力扣 538. 把二叉搜索树转换为累加树 1038. 从二叉搜索树到更大和树

题目来源: 538:https://leetcode.cn/problems/convert-bst-to-greater-tree/description/ 1038: https://leetcode.cn/problems/binary-search-tree-to-greater-sum-tree/description/ C题解1:递归法。二叉搜索树由大到小&#…

用 JAVA 开发游戏连连看(之六)优化:让程序运行更稳定、更高

之六)优化:让程序运行更稳定、更高效 改善游戏的合理性 到目前为止,我们的游戏基本上算是完成了,为了使程序更合理,我们还需要将整个程序从头再理一遍,看看有没有改进的地方。 首先,在变量…