详解 pyinstaller 打包多个py文件
前言
最近有个需求要把python项目打包成可执行程序运行,看了很多帖子,大多数博主都采用pyinstall 打包,看着也不难,本以为很简单的事情,对于我这个新手来说也折腾了2个多小时吧,记录下遇到的坑,和大家分享,希望能给予你们帮助,少走弯路。
安装pyinstall
pip install pyinstaller
实验项目结构
tree /f
查看文件结构
pyinstaller -F -c simple.py -p __init__.py -p test_install.py
打包多个py文件的命令
pyinstaller [主文件] -p [其他文件1] -p [其他文件2] --hidden-import [自建模块1] --hidden-import [自建模块2]
其中sample.py是主程序入口文件,其他.py文件是自建模块(test_install.py)。所以在执行下面命令:
pyinstaller -F -c simple.py -p __init__.py -p test_install.py
执行完成后会产生2个文件夹build,dist和一个文件simple.spec
进入exe目录并成功执行exe程序
OK 这样就完成了打包的程序,事实上,我并不是这么顺利,中间也遇到些问题
遇到的问题 Failed to execute script ‘simple’ due to unhandled exception!
首先看下py文件的内容,内容瞎写的,只是为了测试!!!
test_install.py
def install_test():
print('install test!')
simple.py 调用 test_install.py 中的方法
import test_install
...
test_install.install_test()
from test_install import install_test
...
install_test()
这四种方式用python simple.py
都是能通过的,然而方式一和方式二打包后都有报错ModuleNotFoundError: No module named ‘test_install’
[37320] Failed to execute script ‘simple’ due to unhandled exception!
解决方法
原因 install_test 是在sample包下,导入路径要写上父包的路径
from sample.test_install import install_test
...
install_test()
from sample import test_install
...
test_install.install_test()
遇到的问题 NameError: name ‘exit’ is not defined
解决方法:在simple.py中使用的exit()
替换为sys.exit()
出错的原因exit 用于给交互式 Shell 返回值,而 sys.exit 是用于程序内部
Python 中的 exit() 和 sys.exit() 的区别
exit is a helper for the interactive shell – sys.exit is intended for use in programs.
The site module (which is imported automatically during startup, except if the -S command-line option is given) adds several constants to the built-in namespace (e.g. exit). They are useful for the interactive interpreter shell and should not be used in programs.
Note that there is a third exit option, namely os._exit, which exits without calling cleanup handlers, flushing stdio buffers, etc. (and which should normally only be used in the child process after a fork()).
对于上面的引用的理解:
参考链接
文中测试项目地址
python打包后,执行报错:NameError: name ‘exit‘ is not defined
Pyinstaller打包多个.py文件
python相对路径导入bug解决:ImportError: attempted relative import with no known parent package
来源:csdn__Dong