No module named '_tkinter' error message when using pyinstaller

Viewed 463

I'm trying to use pyinstaller to create a basic Mac app using tkinter. The file itself, saved under the name example.py, is just

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.

# -*- 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.

1 Answers

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

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. File you built with pyinstaller.

Feel free to remove any other folders inside Library but bin, that must stay. 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.

It's simple and easily done. Hope this will help some others which are will face the same issue.

Related