PyInstaller 不导入队列

PyInstaller doesn't import Queue

我正在尝试使用 twisted 和 Queue 编译一些脚本。

pyinstaller sample.py --onefile
# -*- coding: utf-8 -*-#
from twisted import *
import queue as Queue
a = Queue.Queue()

不幸的是,生成的文件失败 ImportError: No module named queue

pip install twisted --upgrade

修复了一切。

更新

也不要忘记在 cmdline 中使用 --hidden-import=queue

我认为这根本不是与 PyInstaller 或 Twisted 相关的问题。 Queue 模块是标准库的一部分,问题在于您如何命名它。在 Python 2 中,它是带有大写字母的 Queue,但在 Python 3 中,它被重命名为 queue 以遵循模块具有小写名称的更标准的命名约定。

您的脚本似乎是 Python 2 代码到 Python 3 的端口(因此是 importas Queue 部分),但您 运行 它与 Python 2 仍然。这可能会以其他更微妙的方式失败,而不仅仅是 Queue 导入错误(例如,它的 Unicode 处理可能完全错误)。

在 windows 使用 Python 2.7,pyinstaller 到 3.2 可能会被具有相似名称的队列和队列模块混淆。 https://github.com/pyinstaller/pyinstaller/issues/1935

这在pyinstaller的主干上是固定的。我必须从源代码安装 pyinstaller 才能解决该错误。

git clone https://github.com/pyinstaller/pyinstaller
cd pyinstaller
python setup.py install

我用这个命名成功了。我用 Python 2.7

import Queue
queue = Queue.Queue()

并使用 pyinstaller 传递此参数:

--hidden-import=Queue

而且有效。