python的PyInstaller库的学习(附:解决pip更新问题、实现文件连接图标打包、科赫雪花小包裹)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34195441/article/details/88042657

1、PyInstaller库概述

将.py源代码转换成无需源代码的可执行文件

                                              -windows(exe文件)

-.py —>   PyInstaller   —>     -Linux

                                              -Mac OS X

PyInstaller库是第三方库

-官方网站:https://www.pyinstaller.org

-第三方库:使用前需要额外安装

-安装第三方库需要使用pip工具(有时会提示更新,解决方法见下面pyinstaller库的安装)

2、PyInstaller库的安装

win + R,输入cmd打开命令行窗口 输入pip install pyinstaller进行安装如下:

安装需要一定时间,最后出现successflly等字样结束。

我安装时出现了提示更新pip的字样:

You are using pip version 10.0.1, however version 19.0.3 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

解决方案:查看了  pip help install ,官方给出 -U -upgrate,于是输入如下,完美解决pip的更新:

之后可输入pyinstaller检查是否安装成功:

3、Pyinstaller库的简单使用

(1)(cmd命令行)打开cmd窗口,把路径切换到文件所在路径(文件随便放在哪里都行)打开命令提示行,输入以下内容(最后的是文件名):pyinstaller-F  <文件名 .py>

如图生成三个文件夹,其中,前两个文件夹可以安全删除掉,在dist文件  夹中会看到一个与原文件同名的exe文件,这个文件就是打包之后生成的文件,我们可以用鼠标双击这个文件执行它。

(2)常用参数

参数 描述
-h 查看帮助
--clean 清理打包过程中的临时文件
-D,--onedir 默认值,生成dist文件夹(一般不推荐使用此方法)
-F,--onefile 在dist文件夹中只生成独立的打包文件
-i <图表文件名.ico> 指定打包程序使用的图标(icon)文件

附:输入参数的含义:

-F 表示生成单个可执行文件

-w 表示去掉控制台窗口,这在GUI界面时非常有用,不过如果是命令行程序的话就把这个选项删除吧

-p 表示你自己自定义需要加载的类路径,一般情况下用不到

-i 表示可执行文件的图标

使用举例

1)利用pyinstaller打包一个py文件,连接图标打开

首先下载一个.ico图标,放在需要被打包的文件相同路径下,输入如下:

D:\Python37\cources>pyinstaller -i o1.ico -F PythonDraw.py

按回车键之后如下:

77 INFO: PyInstaller: 3.4
77 INFO: Python: 3.7.0
78 INFO: Platform: Windows-10-10.0.17763-SP0
87 INFO: wrote D:\Python37\cources\PythonDraw.spec
88 INFO: UPX is not available.
90 INFO: Extending PYTHONPATH with paths
['D:\\Python37\\cources', 'D:\\Python37\\cources']
90 INFO: checking Analysis
105 INFO: Building because inputs changed
106 INFO: Initializing module dependency graph...
108 INFO: Initializing module graph hooks...
110 INFO: Analyzing base_library.zip ...
5173 INFO: running Analysis Analysis-00.toc
5176 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by d:\python37\python.exe
5200 WARNING: lib not found: api-ms-win-crt-math-l1-1-0.dll dependency of d:\python37\python.exe
5224 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of d:\python37\python.exe
5244 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of d:\python37\python.exe
5266 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of d:\python37\python.exe
5286 WARNING: lib not found: api-ms-win-crt-locale-l1-1-0.dll dependency of d:\python37\python.exe
5312 WARNING: lib not found: api-ms-win-crt-convert-l1-1-0.dll dependency of d:\python37\VCRUNTIME140.dll
5332 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of d:\python37\VCRUNTIME140.dll
5351 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of d:\python37\VCRUNTIME140.dll
5374 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of d:\python37\VCRUNTIME140.dll
5393 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of d:\python37\VCRUNTIME140.dll
5582 WARNING: lib not found: api-ms-win-crt-math-l1-1-0.dll dependency of d:\python37\python37.dll
5605 WARNING: lib not found: api-ms-win-crt-convert-l1-1-0.dll dependency of d:\python37\python37.dll
5623 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of d:\python37\python37.dll
5642 WARNING: lib not found: api-ms-win-crt-filesystem-l1-1-0.dll dependency of d:\python37\python37.dll
5666 WARNING: lib not found: api-ms-win-crt-process-l1-1-0.dll dependency of d:\python37\python37.dll
5684 WARNING: lib not found: api-ms-win-crt-time-l1-1-0.dll dependency of d:\python37\python37.dll
5708 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of d:\python37\python37.dll
5728 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of d:\python37\python37.dll
5747 WARNING: lib not found: api-ms-win-crt-locale-l1-1-0.dll dependency of d:\python37\python37.dll
5767 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of d:\python37\python37.dll
5788 WARNING: lib not found: api-ms-win-crt-environment-l1-1-0.dll dependency of d:\python37\python37.dll
5817 WARNING: lib not found: api-ms-win-crt-conio-l1-1-0.dll dependency of d:\python37\python37.dll
5839 INFO: Caching module hooks...
5863 INFO: Analyzing D:\Python37\cources\PythonDraw.py
6241 INFO: Loading module hooks...
6243 INFO: Loading module hook "hook-encodings.py"...
6366 INFO: Loading module hook "hook-pydoc.py"...
6369 INFO: Loading module hook "hook-xml.py"...
6692 INFO: Loading module hook "hook-_tkinter.py"...
6715 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of d:\python37\DLLs\_tkinter.pyd
6737 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of d:\python37\DLLs\_tkinter.pyd
6763 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of d:\python37\DLLs\_tkinter.pyd
6977 INFO: checking Tree
7013 INFO: checking Tree
7048 INFO: Looking for ctypes DLLs
7049 INFO: Analyzing run-time hooks ...
7055 INFO: Including run-time hook 'pyi_rth__tkinter.py'
7064 INFO: Looking for dynamic libraries
7096 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of d:\python37\DLLs\_ssl.pyd
7117 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of d:\python37\DLLs\_ssl.pyd
7141 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of d:\python37\DLLs\_ssl.pyd
7185 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of d:\python37\DLLs\pyexpat.pyd
7204 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of d:\python37\DLLs\pyexpat.pyd
7227 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of d:\python37\DLLs\pyexpat.pyd
7246 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of d:\python37\DLLs\pyexpat.pyd
7266 WARNING: lib not found: api-ms-win-crt-environment-l1-1-0.dll dependency of d:\python37\DLLs\pyexpat.pyd
7306 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of d:\python37\DLLs\_hashlib.pyd
7352 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of d:\python37\DLLs\_lzma.pyd
7387 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of d:\python37\DLLs\_lzma.pyd
7432 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of d:\python37\DLLs\_socket.pyd
7467 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of d:\python37\DLLs\_socket.pyd
7506 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of d:\python37\DLLs\unicodedata.pyd
7540 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of d:\python37\DLLs\unicodedata.pyd
7568 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of d:\python37\DLLs\unicodedata.pyd
7608 WARNING: lib not found: api-ms-win-crt-math-l1-1-0.dll dependency of d:\python37\DLLs\_bz2.pyd
7636 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of d:\python37\DLLs\_bz2.pyd
7663 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of d:\python37\DLLs\_bz2.pyd
7686 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of d:\python37\DLLs\_bz2.pyd
7704 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of d:\python37\DLLs\_bz2.pyd
7739 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of d:\python37\DLLs\select.pyd
7764 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of d:\python37\DLLs\_tkinter.pyd
7788 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of d:\python37\DLLs\_tkinter.pyd
7825 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of d:\python37\DLLs\_tkinter.pyd
7892 WARNING: lib not found: api-ms-win-crt-utility-l1-1-0.dll dependency of d:\python37\DLLs\libssl-1_1-x64.dll
7926 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of d:\python37\DLLs\libssl-1_1-x64.dll
7999 WARNING: lib not found: api-ms-win-crt-time-l1-1-0.dll dependency of d:\python37\DLLs\libssl-1_1-x64.dll
8034 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of d:\python37\DLLs\libssl-1_1-x64.dll
9157 WARNING: lib not found: api-ms-win-crt-convert-l1-1-0.dll dependency of d:\python37\DLLs\libcrypto-1_1-x64.dll
9176 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of d:\python37\DLLs\libcrypto-1_1-x64.dll
9195 WARNING: lib not found: api-ms-win-crt-utility-l1-1-0.dll dependency of d:\python37\DLLs\libcrypto-1_1-x64.dll
9215 WARNING: lib not found: api-ms-win-crt-filesystem-l1-1-0.dll dependency of d:\python37\DLLs\libcrypto-1_1-x64.dll
9238 WARNING: lib not found: api-ms-win-crt-time-l1-1-0.dll dependency of d:\python37\DLLs\libcrypto-1_1-x64.dll
9260 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of d:\python37\DLLs\libcrypto-1_1-x64.dll
9280 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of d:\python37\DLLs\libcrypto-1_1-x64.dll
9299 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of d:\python37\DLLs\libcrypto-1_1-x64.dll
9318 WARNING: lib not found: api-ms-win-crt-environment-l1-1-0.dll dependency of d:\python37\DLLs\libcrypto-1_1-x64.dll
9398 WARNING: lib not found: api-ms-win-crt-environment-l1-1-0.dll dependency of d:\python37\DLLs\tcl86t.dll
9416 WARNING: lib not found: api-ms-win-crt-math-l1-1-0.dll dependency of d:\python37\DLLs\tcl86t.dll
9440 WARNING: lib not found: api-ms-win-crt-utility-l1-1-0.dll dependency of d:\python37\DLLs\tcl86t.dll
9459 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of d:\python37\DLLs\tcl86t.dll
9479 WARNING: lib not found: api-ms-win-crt-convert-l1-1-0.dll dependency of d:\python37\DLLs\tcl86t.dll
9503 WARNING: lib not found: api-ms-win-crt-time-l1-1-0.dll dependency of d:\python37\DLLs\tcl86t.dll
9525 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of d:\python37\DLLs\tcl86t.dll
9544 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of d:\python37\DLLs\tcl86t.dll
9567 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of d:\python37\DLLs\tcl86t.dll
9636 WARNING: lib not found: api-ms-win-crt-math-l1-1-0.dll dependency of d:\python37\DLLs\tk86t.dll
9655 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of d:\python37\DLLs\tk86t.dll
9679 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of d:\python37\DLLs\tk86t.dll
9702 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of d:\python37\DLLs\tk86t.dll
9722 WARNING: lib not found: api-ms-win-crt-time-l1-1-0.dll dependency of d:\python37\DLLs\tk86t.dll
9759 WARNING: lib not found: api-ms-win-crt-convert-l1-1-0.dll dependency of d:\python37\DLLs\tk86t.dll
9778 WARNING: lib not found: api-ms-win-crt-utility-l1-1-0.dll dependency of d:\python37\DLLs\tk86t.dll
9809 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of d:\python37\DLLs\tk86t.dll
9814 INFO: Looking for eggs
9814 INFO: Using Python library d:\python37\python37.dll
9815 INFO: Found binding redirects:
[]
9841 INFO: Warnings written to D:\Python37\cources\build\PythonDraw\warn-PythonDraw.txt
9905 INFO: Graph cross-reference written to D:\Python37\cources\build\PythonDraw\xref-PythonDraw.html
9970 INFO: checking PYZ
9975 INFO: Building because name changed
9976 INFO: Building PYZ (ZlibArchive) D:\Python37\cources\build\PythonDraw\PYZ-00.pyz
10821 INFO: Building PYZ (ZlibArchive) D:\Python37\cources\build\PythonDraw\PYZ-00.pyz completed successfully.
10840 INFO: checking PKG
10854 INFO: Building because name changed
10854 INFO: Building PKG (CArchive) PKG-00.pkg
14342 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
14395 INFO: Bootloader d:\python37\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
14396 INFO: checking EXE
14398 INFO: Building EXE because EXE-00.toc is non existent
14399 INFO: Building EXE from EXE-00.toc
14486 INFO: SRCPATH [('o1.ico', None)]
14486 INFO: Updating icons from ['o1.ico'] to C:\Users\lenovo\AppData\Local\Temp\tmpfv066g30
14496 INFO: Writing RT_GROUP_ICON 0 resource with 146 bytes
14497 INFO: Writing RT_ICON 1 resource with 1640 bytes
14499 INFO: Writing RT_ICON 2 resource with 744 bytes
14500 INFO: Writing RT_ICON 3 resource with 296 bytes
14505 INFO: Writing RT_ICON 4 resource with 3752 bytes
14506 INFO: Writing RT_ICON 5 resource with 2216 bytes
14506 INFO: Writing RT_ICON 6 resource with 1384 bytes
14509 INFO: Writing RT_ICON 7 resource with 67624 bytes
14510 INFO: Writing RT_ICON 8 resource with 9640 bytes
14514 INFO: Writing RT_ICON 9 resource with 4264 bytes
14515 INFO: Writing RT_ICON 10 resource with 1128 bytes
14596 INFO: Appending archive to EXE D:\Python37\cources\dist\PythonDraw.exe
14756 INFO: Building EXE from EXE-00.toc completed successfully.

感觉缺失了很多文件,但是根据最后所显示的路径,生成如下,运行成功:

2)科赫雪花小包裹

科赫曲线,也叫雪花曲线

科赫曲线的绘制

#KochDrawV1.py
import turtle
def koch(size,n):
    if n == 0:
        turtle.fd(size)
    else:
        for angle in [0,60,-120,60]:
            turtle.left(angle)
            koch(size/3,n-1)
def main():
    turtle.setup(800,400)
    turtle.penup()
    turtle.goto(-300,-50)
    turtle.pendown()
    turtle.pensize(2)
    koch(600,3)                #3阶科赫曲线,阶数
    turtle.hideturtle()
main()
#KochDrawV2.py
import turtle
def koch(size,n):
    if n == 0:
        turtle.fd(size)
    else:
        for angle in [0,60,-120,60]:
            turtle.left(angle)
            koch(size/3,n-1)
def main():
    turtle.setup(600,600)
    turtle.penup()
    turtle.goto(-200,100)
    turtle.pendown()
    turtle.pensize(2)
    level = 3
    koch(400,level)                #3阶科赫曲线,阶数
    turtle.right(120)
    koch(400,level)
    turtle.right(120)
    koch(400,level)
    turtle.hideturtle()
main()

运行结果:

打包方式同上

猜你喜欢

转载自blog.csdn.net/qq_34195441/article/details/88042657
今日推荐