且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Pyinstaller 无法导入 scipy.signals

更新时间:2023-02-26 19:10:32

我在打包使用 xarray 的脚本时遇到了类似的问题.解决方案是找出哪个包正在导入 jupyter,并将其添加到 excluded-modules 列表中.就我而言,我排除 dask,但在您的情况下,情况会有所不同.

I had a similar issue when packaging up an script that used xarray. The solution was to find out which package was importing jupyter, and add it to the list of excluded-modules. In my case, I excluded dask, but in your case things will be different.

我将使用 --log-level DEBUG 重新运行您的 pyinstaller 命令以获得更多日志信息,然后查看 build/ 中生成的日志文件em> 目录.如果您有 graphviz,或者知道如何使用 graphs,那么您可以分析 .dot 文件以了解安装 em>jupyter/IPython.如果您不知道如何使用 graphviz,没问题,只需在浏览器中打开 html 文件即可.该文件显示了您的脚本导入的所有包,以及这些包导入的所有包,以及由这些包导入的所有包,等等.注意,这是一个非常复杂的包 A 导入网络...包 A 是由 ... 导入的,所以不要迷失在细节中.

I would rerun your pyinstaller command with --log-level DEBUG in order to get some more logging information, then take a look at the log files generated in the build/ directory. If you have graphviz, or know how to work with graphs, then you can analyze the .dot file to learn about the packages that install jupyter/IPython. If you don't know how to use graphviz, no problem, just open up the html file in a browser. This file shows all of the packages imported by your script, and all of the packages that those packages import, and are imported by, etc. Beware, this is a really complex web of package A imports ... and package A is imported by ..., so don't get lost in the details.

此处的目标是找到从您的脚本到导致此问题的 IPython/jupyter 的导入的路径.您想在您的脚本看起来合乎逻辑的任何地方停止此路径.在你的情况下,许多 scipy 子包导入 matplotlib.pyplot,它导入 IPython,它导入最终导入 jupyter_client代码>.您希望通过排除其中一个包来尽早切断从脚本到 jupyter/IPython 的路径.您决定排除的包将取决于您的应用.

The goal here is to find the path of imports that leads from your script to IPython/jupyter that is leading to this issue. You want to stop this path wherever seems logical for your script. In your case, many of the scipy subpackages import matplotlib.pyplot, which imports IPython, which imports things which eventually import jupyter_client. You want to sever this path from your script to jupyter/IPython as early as you can by excluding one of the packages. The package you decide to exclude will depend on your app.

消除此错误的最简单方法是通过 排除 matplotlib 将这个 path 从您的脚本切断​​到 jupyter代码>.但是,如果您在脚本中使用 matplotlib 来生成绘图,则不能这样做.您将不得不选择更下游的包.最终你可以排除 IPython,这应该可以解决问题.查看 pyinstaller 文档以了解如何使用 --exclude-module 参数:

The easiest way to get rid of this error would be to sever this path from your script to jupyter by excluding matplotlib. However, if you use matplotlib in your script to generate plots, then you can't do that. You would have to pick a package that is more downstream. Ultimately you could exclude IPython, and that should do the trick. Take a look at the pyinstaller documentation for how to use the --exclude-module argument:

https://pyinstaller.readthedocs.io/en/stable/usage.html#what-to-bundle-where-to-search

或者这个*** question有更多信息:

Or this *** question which has some more info:

如何从 pyInstaller 中删除/排除模块和文件?

附带说明一下,您应该开始习惯排除不必要的包,因为这可以显着减小捆绑应用的大小并提高性能.

On a side note, you should start getting comfortable excluding unnecessary packages since this can drastically decrease the size of your bundled app and improve performance.