脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Python - pyinstaller pathex参数引发打包no module name异常

pyinstaller pathex参数引发打包no module name异常

2022-12-29 11:14iSZ Python

这篇文章主要为大家介绍了一个关于pyinstaller的 pathex 参数所引发的打包执行报no module name的异常错误解决,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

现象:

最近将pyinsatller升级到最新的Version: 5.0.1版本后(之前一直用的是3.5版本同样方法未遇到问题,今次更新到最新版本后5.0.1后打包就遇到问题,具体是这中间哪个版本开始有变化也不清楚了,也不去追究,凡是在新版本中遇到问题就在新版本中解决),详细现象及解决办法如下:

1。 先贴上pyinstaller的官方介绍:点击跳转

2。使用pyinstaller打包完成后到dist目录下点击 .exe程序运行时提示 “no module found”,凡是所有外部加载的模块全部无法识别到,然后单独将对应的包放到该dist目录下再运行则问题消失,表明在打包时未正常对应目录所依赖的模块加载进去

pyinstaller pathex参数引发打包no module name异常

3。先来看打包命令,我是使用批处理,跟 .py的主文件(即可运行主窗口文件)在同一目录

?
1
2
3
4
5
6
7
8
9
10
11
@echo off
echo Current Dish:%~d0
echo Current Dish and Path:%~dp0
echo Current CMD default Dir:"%cd%"
set "current_dir=%~dp0"
echo %current_dir%
set "output_dir=%current_dir%"
cd %output_dir%
;echo "%cd%"
pyinstaller.exe -D -w %current_dir%\SmartTools.py -i %current_dir%\icon\Tool.ico
pause

4。pyinstaller打包的时候会首先生成一个spec文件,我们可以手工去修改这个文件,下次打包指定用这个文件。它的好处是:里面可以写更复杂的选项,也可以重复利用,这个方法我是在pyinstaller 3.5版本中这么用的,但在pyinstaller5.0.1版本中却会每次重新覆盖掉我所修改的spec文件,导致该pathex每次为空,所以在打包时就不会将py文件中所引用的文件加载进去进行编辑打包,关于该pathex参数解释可以到第一步的官方中去查找,下面我截图出来给你们看(明确告诉我们该参数主要就是文件中关于imports导模块所需路径)

pyinstaller pathex参数引发打包no module name异常

5。那到这里就已经知道问题出在哪里了,但要如何处理才能将spec文件中的pathex中带上当前地址呢,上面也已经给出的明确的答案,就是在pyinstaller后面带上 --paths参数,现修改编辑打包文件如下(红色字体为新增部分):

?
1
2
3
4
5
6
7
8
9
10
11
@echo off
echo Current Dish:%~d0
echo Current Dish and Path:%~dp0
echo Current CMD default Dir:"%cd%"
set "current_dir=%~dp0"
echo %current_dir%
set "output_dir=%current_dir%"
cd %output_dir%
;echo "%cd%"
pyinstaller.exe --paths %current_dir% -D -w %current_dir%\SmartTools.py -i %current_dir%\icon\Tool.ico
pause

6。重新运行该批处理后再去检查 spec文件,查看pathex是否已经将对应目录添加上,经确认该参数中已经有值了,为一个列表

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# -*- mode: python ; coding: utf-8 -*-
 block_cipher = None
 a = Analysis(
     ['E:\\SmartTools\\\\SmartTools.py'],
     pathex=['E:\\SmartTools\\'],
     binaries=[],
     datas=[],
     hiddenimports=[],
     hookspath=[],
     hooksconfig={},
     runtime_hooks=[],
     excludes=[],
     win_no_prefer_redirects=False,
     win_private_assemblies=False,
     cipher=block_cipher,
     noarchive=False,
 )

7。 重新到dist目录下去执行 .exe程序,正常启动未报任何错误,到此问题成功解决。

以上就是pyinstaller pathex参数引发打包no module name异常的详细内容,更多关于pyinstaller打包异常的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/aziji/p/16254288.html

延伸 · 阅读

精彩推荐