简体   繁体   English

使用 pyinstaller 时没有名为“_tkinter”的模块错误消息

[英]No module named '_tkinter' error message when using pyinstaller

I'm trying to use pyinstaller to create a basic Mac app using tkinter.我正在尝试使用 pyinstaller 创建使用 tkinter 的基本 Mac 应用程序。 The file itself, saved under the name example.py, is just以 example.py 名称保存的文件本身只是

from tkinter import Tk,Label
window = Tk()
window.title("My Window")
lbl = Label(window, text="Hello World")
lbl.grid(column=0, row=0)
window.mainloop()

Very simple.很简单。 Now现在

pyinstaller example.py --onefile --windowed

produces an app bundle, but I'm unable to run the program from the terminal using生成一个应用程序包,但我无法使用从终端运行程序

./dist/example.app/Contents/MacOS/example

due to the the error message由于错误消息

ModuleNotFoundError: No module named '_tkinter'

The spec file looks like this, where I've edited to include tkinter as a hidden import.规范文件看起来像这样,我在其中编辑了 tkinter 作为隐藏导入。

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None

a = Analysis(['pyinstaller_basic'],
     pathex=['/Users/fishbacp/Desktop/Python_May_2021'],
     binaries=[],
     datas=[],
     hiddenimports=['tkinter'],
     hookspath=[],
     runtime_hooks=[],
     excludes=[],
     win_no_prefer_redirects=False,
     win_private_assemblies=False,
     cipher=block_cipher,
     noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
      cipher=block_cipher)
exe = EXE(pyz,
      a.scripts,
      a.binaries,
      a.zipfiles,
      a.datas,
      [],
      name='example',
      debug=False,
      bootloader_ignore_signals=False,
      strip=False,
      upx=True,
      upx_exclude=[],
      runtime_tmpdir=None,
      console=False )
app = BUNDLE(exe,
      name='example.app',
      icon=None,
      bundle_identifier=None)

Now I create the app using现在我使用创建应用程序

pyinstaller example.spec example.py --onefile --windowed

Still, I get the same error message when trying to run the app from the terminal.尽管如此,我在尝试从终端运行应用程序时仍收到相同的错误消息。

I'm using Python 3.9 on a Mac running Big Sur.我在运行 Big Sur 的 Mac 上使用 Python 3.9。

I have just faced the same issue and solved it.我刚刚遇到同样的问题并解决了。

If you use virtual enviroment you should go in your enviroment for example mine: D:\GitHub\SnA-KPIS-JSON-Editor\20_Implementation\venv如果您使用虚拟环境,您应该在您的环境中使用 go,例如我的:D:\GitHub\SnA-KPIS-JSON-Editor\20_Implementation\venv

If you use system/allFor Python go inside your Python, path could look like this: C:\Users\YourLogin\Programs\PythonX.X If you use system/allFor Python go inside your Python, path could look like this: C:\Users\YourLogin\Programs\PythonX.X

Inside in the Python folder copy folder named Library and just copy it inside your folder with.exe file.在 Python 文件夹中复制名为Library的文件夹,然后将其复制到您的文件夹中 with.exe 文件。 File you built with pyinstaller.您使用 pyinstaller 构建的文件。

Feel free to remove any other folders inside Library but bin , that must stay.随意删除Library中的任何其他文件夹,但bin必须保留。 It's key to solve the issue.是解决问题的关键。

If you have images which are used in tkinter you have to copy and paste them inside your build folder as well.如果您有在 tkinter 中使用的图像,您还必须将它们复制并粘贴到您的构建文件夹中。

It's simple and easily done.这很简单,很容易完成。 Hope this will help some others which are will face the same issue.希望这将有助于其他一些将面临同样问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM