目录

Python打包,嵌入式方式打包,而且是官方推荐的打包方式-Python嵌入式打包真的不如pyinstaller吗

Python打包,嵌入式方式打包,而且是官方推荐的打包方式-Python嵌入式打包真的不如pyinstaller吗?

嵌入式打包:

解释:

用嵌入式版本的python解释器进行打包程序,python有嵌入式版本,而且这个嵌入式版本的解释器大小很小,可以很好的省磁盘空间!!!

打包步骤:

1、从python官网下载嵌入式版本的python

[Welcome to Python.org

The official home of the Python Programming Language

https://csdnimg.cn/release/blog_editor_html/release2.3.7/ckeditor/plugins/CsdnLink/icons/icon-default.png?t=O83A “Welcome to Python.org”) https://i-blog.csdnimg.cn/blog_migrate/029b40d41bf15eee33662e12f7fda7d8.png

下载网址:

[Python Releases for Windows | Python.org

The official home of the Python Programming Language

https://csdnimg.cn/release/blog_editor_html/release2.3.7/ckeditor/plugins/CsdnLink/icons/icon-default.png?t=O83A “Python Releases for Windows | Python.org”) https://i-blog.csdnimg.cn/blog_migrate/93423448f1cdcc86784abdc89694d3f7.png

下载下来,放在一个文件夹里:

https://i-blog.csdnimg.cn/blog_migrate/0b8a10402ec9f17ecb6ff00ab10ed2e1.png

2、配置环境

解压该压缩包,进入该文件夹里:

https://i-blog.csdnimg.cn/blog_migrate/43edce75b4317161ee0bcedc85e32414.png

https://i-blog.csdnimg.cn/blog_migrate/9f955118af53c95fa3bd7c1c52725578.png

更改python310._pth文件里面的内容:

https://i-blog.csdnimg.cn/blog_migrate/b0bcce09e5ee732519959207a1be51f0.png

https://i-blog.csdnimg.cn/blog_migrate/05df902b2422f1d4a7210ce85732fd5a.png

https://i-blog.csdnimg.cn/blog_migrate/25482bbee848629571acaf2562ef3106.png

下载get-pip.py文件:

[Index of /pip/

https://csdnimg.cn/release/blog_editor_html/release2.3.7/ckeditor/plugins/CsdnLink/icons/icon-default.png?t=O83A “Index of /pip/”) https://i-blog.csdnimg.cn/blog_migrate/d4445a448b5dfb078d48a8cdad309674.png

将get-pip.py放到python解释器同级目录:

https://i-blog.csdnimg.cn/blog_migrate/45e1091506cbd270d92e17d51d096282.png

进入C:\Users\10501\Desktop\RunPythonWork\python-3.10.11-embed-amd64该文件夹,打开cmd:

https://i-blog.csdnimg.cn/blog_migrate/2d31a98707828d75eb98f48cc3056ae0.png

执行如下命令:

.\python.exe get-pip.py

https://i-blog.csdnimg.cn/blog_migrate/c73506dd5a3ad14cc053030f245208e7.png

这样就会生成两个文件夹Lib、Scripts:

https://i-blog.csdnimg.cn/blog_migrate/820376cc3e26c4251fda1c806610d70e.png

进入Scripts文件夹,打开cmd:

https://i-blog.csdnimg.cn/blog_migrate/7a7b5bc37937c3a2d801bee723f9617a.png

https://i-blog.csdnimg.cn/blog_migrate/4e9018e34a40c09f979a50d3abcb5755.png

用这个文件夹的pip进行安装你自己的程序所需要的第三方库:

https://i-blog.csdnimg.cn/blog_migrate/ce3c7252a5501b7f226ba6f17ee5127e.png

现在可以查看嵌入式pyhthon文件夹的大小:

https://i-blog.csdnimg.cn/blog_migrate/2afc4fbafec00a191b40116dc761b050.png

这已经很小了,相比于非嵌入式版本的python小很多了。

3、将自己写好的程序文件夹放入与python-3.10.11-embed-amd64同级目录下:

https://i-blog.csdnimg.cn/blog_migrate/24447464c6b2d4c1674b1a5b3cc4b4c8.png

4、用嵌入式python执行写好的程序源代码:

.\python-3.10.11-embed-amd64\python.exe .\PackageTest\main\main.py

https://i-blog.csdnimg.cn/blog_migrate/4d1538c7447a3d2faf1aa0b75f2cc116.png

5、编写bat文件去直接执行命令:

start.bat:

@echo off


chcp 65001

echo 开始运行


.\python-3.10.11-embed-amd64\python.exe .\PackageTest\main\main.py

pause

https://i-blog.csdnimg.cn/blog_migrate/5eb1f57da9d0d090ce1093b0857dd03d.png

点击start.bat运行:

https://i-blog.csdnimg.cn/blog_migrate/266c0cbee9c740ae6fe39ac2404e3fc7.png

注意bug解决:

1、 C:\Users\10501\Desktop\RunPythonWork>.\python-3.10.11-embed-amd64\python.exe .\PackageTest\main\main.py Traceback (most recent call last): File “C:\Users\10501\Desktop\RunPythonWork\PackageTest\main\main.py”, line 15, in from PackageTest.utils.utils import * ModuleNotFoundError: No module named ‘PackageTest’

https://i-blog.csdnimg.cn/blog_migrate/288298f9d3918add11dd654aee1d38a4.png

报错:找不到自己创建的包!!!

main.py的代码如下:

# ---encoding:utf-8---
# @Time    : 2023/6/22 17:35
# @Author  : CBAiotAigc
# @Email   :1050100468@qq.com
# @Site    : 
# @File    : main.py
# @Project : Test
# @Software: PyCharm
# import os
# import sys
# BASE_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# print(BASE_PATH)
# sys.path.append(BASE_PATH)

from PackageTest.utils.utils import *

print("main")

上面的PackageTest是我创建的包:

https://i-blog.csdnimg.cn/blog_migrate/655e68ad49014884a7bbc527807d8bf2.png

解决方案:

代码最上面添加如下代码,即将sys.path追加项目根目录:

import os
import sys
BASE_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
print(BASE_PATH)
sys.path.append(BASE_PATH)

完整的main.py如下:

# ---encoding:utf-8---
# @Time    : 2023/6/22 17:35
# @Author  : CBAiotAigc
# @Email   :1050100468@qq.com
# @Site    : 
# @File    : main.py
# @Project : Test
# @Software: PyCharm
import os
import sys
BASE_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
print(BASE_PATH)
sys.path.append(BASE_PATH)

from PackageTest.utils.utils import *

print("main")

再次运行则可以成功:

https://i-blog.csdnimg.cn/blog_migrate/97bbd0a97bab0473e0e2cbae15799aba.png

大功告成!!!