【发布时间】:2017-05-18 00:07:08
【问题描述】:
我已经使用 Tkinter 创建了一个 python 脚本,并尝试使用带有 Pyinstaller 的 Mac 打包它。 Pyinstaller 创建可执行文件,但是当我尝试运行它时说 Tkinter 模块丢失。我意识到在 SO 上有几个类似的问题,但没有一个解决方案对我有用。
这是我的main.py 脚本的顶部:
#!/usr/bin/env python
import sys
import os
import traceback
import json
import time
import Tkinter
这是我的 pyinstaller 版本:
$ pyinstaller --version
3.3.dev0+483c819
我用这个命令运行 pyinstaller:
$ pyinstaller main.py
这是该命令的输出:
30 INFO: PyInstaller: 3.3.dev0+483c819
30 INFO: Python: 3.4.3
34 INFO: Platform: Darwin-15.6.0-x86_64-i386-64bit
35 INFO: wrote /Users/jonathanwilson/Documents/Projects/hr_digitization_tool/hr-records-management-tool-no-container/main.spec
36 INFO: UPX is not available.
38 INFO: Extending PYTHONPATH with paths
['/Users/jonathanwilson/Documents/Projects/hr_digitization_tool/hr-records-management-tool-no-container',
'/Users/jonathanwilson/Documents/Projects/hr_digitization_tool/hr-records-management-tool-no-container']
38 INFO: checking Analysis
38 INFO: Building Analysis because out00-Analysis.toc is non existent
38 INFO: Initializing module dependency graph...
39 INFO: Initializing module graph hooks...
40 INFO: Analyzing base_library.zip ...
1395 INFO: Processing pre-find module path hook distutils
2445 INFO: running Analysis out00-Analysis.toc
2452 INFO: Caching module hooks...
2454 INFO: Analyzing /Users/jonathanwilson/Documents/Projects/hr_digitization_tool/hr-records-management-tool-no-container/main.py
2499 INFO: Loading module hooks...
2499 INFO: Loading module hook "hook-encodings.py"...
2553 INFO: Loading module hook "hook-pydoc.py"...
2553 INFO: Loading module hook "hook-xml.py"...
2753 INFO: Loading module hook "hook-distutils.py"...
2766 INFO: Looking for ctypes DLLs
2766 INFO: Analyzing run-time hooks ...
2773 INFO: Looking for dynamic libraries
2836 INFO: Looking for eggs
2836 INFO: Using Python library /Library/Frameworks/Python.framework/Versions/3.4/Python
2838 INFO: Warnings written to /Users/jonathanwilson/Documents/Projects/hr_digitization_tool/hr-records-management-tool-no-container/build/main/warnmain.txt
2861 INFO: checking PYZ
2861 INFO: Building PYZ because out00-PYZ.toc is non existent
2861 INFO: Building PYZ (ZlibArchive) /Users/jonathanwilson/Documents/Projects/hr_digitization_tool/hr-records-management-tool-no-container/build/main/out00-PYZ.pyz
3175 INFO: checking PKG
3176 INFO: Building PKG because out00-PKG.toc is non existent
3176 INFO: Building PKG (CArchive) out00-PKG.pkg
3185 INFO: Bootloader /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/PyInstaller/bootloader/Darwin-64bit/run
3185 INFO: checking EXE
3185 INFO: Building EXE because out00-EXE.toc is non existent
3185 INFO: Building EXE from out00-EXE.toc
3185 INFO: Appending archive to EXE /Users/jonathanwilson/Documents/Projects/hr_digitization_tool/hr-records-management-tool-no-container/build/main/main
3187 INFO: Fixing EXE for code signing /Users/jonathanwilson/Documents/Projects/hr_digitization_tool/hr-records-management-tool-no-container/build/main/main
3192 INFO: checking COLLECT
3192 INFO: Building COLLECT because out00-COLLECT.toc is non existent
3192 INFO: Building COLLECT out00-COLLECT.toc
当我运行可执行文件时,会打开一个带有以下输出的终端窗口:
Last login: Tue Jan 3 11:35:51 on ttys004
/Users/jonathanwilson/Documents/Projects/hr_digitization_tool/hr-records-management-tool-no-container/dist/main/main ; exit;
[11:43:24][~]$ /Users/jonathanwilson/Documents/Projects/hr_digitization_tool/hr-records-management-tool-no-container/dist/main/main ; exit;
Traceback (most recent call last):
File "main.py", line 7, in <module>
ImportError: No module named 'Tkinter'
Failed to execute script main
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[Process completed]
我尝试使用--hidden-import Tkinter 运行 pyinstaller 命令,但这仍然不起作用。我已经检查了 pyinstaller 文档部分“当事情出错时”,但仍然没有看到解决方案。
【问题讨论】:
-
在python3中,Tkinter被重命名为tkinter。
-
@BryanOakley 但这个问题被标记为
python-2.7。您还必须安装 Python 模块,这可以通过 Pip 完成。在命令行运行pip install tkinter。 -
是的,但看起来 pyinstaller 正在选择 python 3:
30 INFO: Python: 3.4.3而且我认为您不能使用 pip 安装 tkinter。 -
@BryanOakley 我想使用 python2.7 Tkinter 模块,所以我需要让 pyinstaller 使用 python2.7 而不是 python3.4?
-
pyinstaller是用 Python 编写的,因此您可以在编辑器中打开它并查看它是如何工作的。我的第一行有#!/usr/bin/python3.5。在 Linux 上找到它我可以使用which pyinstaller- 也许它也可以在 Mac 上工作。如果你已经安装了 Python 2 的版本,你也可以试试python -m PyInstaller(上面有P和I)。
标签: python python-2.7 tkinter pyinstaller