0

I have a pyinstaller problem with tkinter. The program below works with python3 but does NOT with pyinstaller. I think the problem is a kind of version conflict because I updated python by downloading it from the python.org.

cxfreeze suggests below, even though it works with! import _tkinter # If this fails your Python may not be configured for Tk

Some google suggests there is "hook" error, which I have no idea of.

I am completely stacked here.

My questions:

  1. Is there any ways to fix this error?
  2. Do we have any option --onefile (as in PyInstaller) in py2app as well? (py2app successfully enables tk.)
  3. Is there any ways to clean-install python3 so that I can try this in a clean environment?

Details: The Minimim Code below does not work in case of pyinstaller. It works with no problem with python3.

% python3 no_module_found_error.py.  # just create a window with no problem.

Minimal Code: no_module_found_error_tkinter.py

import tkinter
window = tkinter.Tk()
window.geometry("400x300")
window.title("NO Module Found Error: _tkinter")
btn = tkinter.Button(window, text="button")
btn.place(x=125, y=230, width=150, height=40)
window.mainloop()

Once pyinstaller was applied, IT DOES NOT WORK.

% pyinstaller no_module_found_error_tkinter.py --onefile --windowed --hidden-import=tkinter  
% dist/no_module_found_error_tkinter

Traceback (most recent call last):
   File "no_module_found_error_tkinter.py", line 1, in <module>
    import tkinter
   File "PyInstaller/loader/pyimod02_importers.py", line 499, in exec_module
   File "tkinter/__init__.py", line 38, in <module>
 ModuleNotFoundError: No module named '_tkinter'
[43491] Failed to execute script 'no_module_found_error_tkinter' due to unhandled exception: No module named '_tkinter'
[43491] Traceback:
Traceback (most recent call last):
   File "no_module_found_error_tkinter.py", line 1, in <module>
    import tkinter
   File "PyInstaller/loader/pyimod02_importers.py", line 499, in exec_module
   File "tkinter/__init__.py", line 38, in <module>
ModuleNotFoundError: No module named '_tkinter'

macOS 12.6 Monterey. Note: tk from macOS 12.6 is somehow buggy, for example properTree does not work properly and python update is suggested, and that is why python was updated in the first place. (properTree did work after the update.)

% python3 -V
Python 3.10.7
% pyinstaller -v      
5.7.0

cxfreeze suggests import _tkinter # If this fails your Python may not be configured for Tk

Google search suggests:

% pip install tk
Requirement already satisfied: tk in  /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (0.1.0)

% pip install tk-dev
ERROR: Could not find a version that satisfies the requirement tk-dev (from  versions: none)
ERROR: No matching distribution found for tk-dev

% pip install tcl   
Requirement already satisfied: tcl in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (0.2)

% pip install tcl8
ERROR: Could not find a version that satisfies the requirement tcl8 (from versions: none)
ERROR: No matching distribution found for tcl8

Note: I am not sure that pyinstaller with tkinter works properly BEFORE python is updated, since I did not try this before the update.

Thank you very much for your help! Have a nice day!

Doctor-KK.

Reply to the comment below:

Thank you very much for your reply.

There are many possible solutions in the post you suggested. I tried just one by one. Nothing worked.

Summary of what I did:

#1. % pyinstaller no_module_found_error_tkinter.py --onefile --windowed --hidden-import=tkinter --hidden-impo#rt=_tkinter -F

#2. % rm -rf ./build ./dist *.spec and then command #1.

#3. modify no_module_found_error_tkinter.spec to modify pathex

a = Analysis(
    ['no_module_found_error_tkinter.py'],
    pathex=["/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (0.1.0)"],
    binaries=[],
    datas=[],
    hiddenimports=['tkinter', '_tkinter'],

#4. there is no __pscache__ directory.

#5. No virtual environment.

I don't know how I can remove __init__.py which is also mentioned in the post without how to do it exactly.

Thank you very much! Have a nice day :)

doctor
  • 107
  • 1
  • 8
  • 1
    Does this solve your problem: https://stackoverflow.com/questions/15114695/imported-module-not-found-in-pyinstaller ? – Gibus Dec 27 '22 at 11:22
  • Thank you very much for your reply. There are many possible solutions in the post you suggested. I tried just one by one, but Nothing worked. Summary of what I did is added in the end of my original post. Thank you very much for your help! Have a nice day :) – doctor Dec 27 '22 at 14:26

0 Answers0