详解 pyinstaller 打包多个py文件

前言

最近有个需求要把python项目打包成可执行程序运行,看了很多帖子,大多数博主都采用pyinstall 打包,看着也不难,本以为很简单的事情,对于我这个新手来说也折腾了2个多小时吧,记录下遇到的坑,和大家分享,希望能给予你们帮助,少走弯路。

安装pyinstall

pip install pyinstaller

实验项目结构

  • 在sampleproject目录下执行 tree /f查看文件结构
    详解 pyinstaller 打包多个py文件
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

详解 pyinstaller 打包多个py文件

进入exe目录并成功执行exe程序

详解 pyinstaller 打包多个py文件

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()).

对于上面的引用的理解:

  • exit()/quit(), 抛出 SystemExit 异常. 一般在交互式 Shell 中退出时使用.

  • sys.exit(n) 退出程序引发 SystemExit 异常, 可以捕获异常执行些清理工作. n 默认值为 0, 表示正常退出. 其他都是非正常退出. 还可以 sys.exit(“sorry, goodbye!”); 一般主程序中使用此退出.

  • os._exit(n), 直接退出, 不抛异常, 不执行相关清理工作. 常用在子进程的退出.

参考链接

文中测试项目地址
python打包后,执行报错:NameError: name ‘exit’ is not defined
Pyinstaller打包多个.py文件
python相对路径导入bug解决:ImportError: attempted relative import with no known parent package

Original: https://blog.csdn.net/BXD1314/article/details/125226289
Author: @左左@右右
Title: 详解 pyinstaller 打包多个py文件

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/731894/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球