New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAC OS X: Tkinter import doesn't copy Tcl and TK #1350

Open
wahid opened this Issue Jul 27, 2015 · 57 comments

Comments

Projects
None yet
@wahid
Copy link

wahid commented Jul 27, 2015

On mac PyInstaller doesn't seem to copy Tcl and Tk once both are copied to the folder the error is gone.

module = imp.load_module(fullname, fp, filename, self._c_ext_tuple)
ImportError: dlopen(_tkinter.so, 2): Library not loaded: @loader_path/Tcl
  Referenced from: _tkinter.so
  Reason: image not found
@htgoebel

This comment has been minimized.

Copy link
Member

htgoebel commented Jul 27, 2015

@htgoebel htgoebel added the need info label Jul 27, 2015

@wahid

This comment has been minimized.

Copy link

wahid commented Jul 27, 2015

The error occurs when running the application.

Python: 2.7.10
PyInstaller: 2.1
Mac OSX: Yosemite 10.10.4

You can reproduce the error by creating a script with:

from Tkinter import *

root = Tk()
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
    exec(bytecode, module.__dict__)
  File "/Users/jphnghknbv/Desktop/python_script/build/script/out00-PYZ.pyz/Tkinter", line 39, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PyInstaller/loader/pyi_importers.py", line 409, in load_module
    module = imp.load_module(fullname, fp, filename, self._c_ext_tuple)
ImportError: dlopen(/Users/jphnghknbv/Desktop/python_script/dist/script/_tkinter.so, 2): Library not loaded: @loader_path/Tcl
  Referenced from: /Users/jphnghknbv/Desktop/python_script/dist/script/_tkinter.so
  Reason: image not found
@htgoebel

This comment has been minimized.

Copy link
Member

htgoebel commented Jul 27, 2015

Please try the latest development version.

@htgoebel htgoebel added v2.1 and removed need info labels Jul 27, 2015

@wahid

This comment has been minimized.

Copy link

wahid commented Jul 27, 2015

Tried development version, but it still gives a error. It seems it doesn't copy executables but 2 folders Tcl and Tk. The 2.1 version was copying these folders to _MEI folder.

>Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PyInstaller-2.1.1.dev0-py2.7.egg/PyInstaller/loader/pyi_importers.py", line 271, in load_module
    exec(bytecode, module.__dict__)
  File "/Users/jphnghknbv/Desktop/test/build/test/out00-PYZ.pyz/Tkinter", line 39, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PyInstaller-2.1.1.dev0-py2.7.egg/PyInstaller/loader/pyi_importers.py", line 414, in load_module
    module = imp.load_module(fullname, fp, filename, self._c_ext_tuple)
ImportError: dlopen(/Users/jphnghknbv/Desktop/test/dist/test/_tkinter.so, 2): Library not loaded: @loader_path/Tcl
  Referenced from: /Users/jphnghknbv/Desktop/test/dist/test/_tkinter.so
  Reason: no suitable image found.  Did find:
    /Users/jphnghknbv/Desktop/test/dist/test/Tcl: not a file
    /Users/jphnghknbv/Desktop/test/dist/test/Tcl: not a file
@stefsmeets

This comment has been minimized.

Copy link

stefsmeets commented Sep 13, 2015

I'm encountering the same problem. Running pyinstaller (2.1) on:

import Tkinter
Tkinter._test()

results in the same errors described by wahid. Is there a work-around for this?

@wahid

This comment has been minimized.

Copy link

wahid commented Sep 13, 2015

What has worked for me is copying Tcl binary from "/System/Library/Frameworks/Tcl.framework" and Tk binary from "/System/Library/Frameworks/Tk.framework" to the output folder after the process completed.

@htgoebel

This comment has been minimized.

Copy link
Member

htgoebel commented Sep 13, 2015

Sorry, I pointed you to the wrong version to use. Please use the current development version from the python3 branch. See https://github.com/pyinstaller/pyinstaller/wiki#downloads for installation instructions.

@stefsmeets

This comment has been minimized.

Copy link

stefsmeets commented Sep 13, 2015

@htgoebel Using the python3 version results in the same errors.
@wahid This seems to work well, thanks!

@htgoebel htgoebel added the OS X label Sep 13, 2015

@stefsmeets

This comment has been minimized.

Copy link

stefsmeets commented Sep 24, 2015

Just for your information, I reinstalled python via homebrew, following a suggestion elsewhere, and it magically solves this problem. I don't like this solution, but with it everything runs as I would expect. I was using the standard PSF python before.

@matysek

This comment has been minimized.

Copy link
Member

matysek commented Sep 24, 2015

@stefsmeets Do you know if python from homebrew uses tcl/tk library from the OS or it compiles it's own version?

My guess is this:

  • Pyinstaller does not bundle tcl/tk from the system Python, because it is considered as a system library.
  • PyInstaller however modifies the path where _tkinter.so will load tcl/tk library. It modifies it to: @loader_path/Tcl - it means look for Tcl in the same directory as the final executable.
  • The fix would be either:
    • bundle the system version of tcl/tk or
    • set the path where _tkinter.so is looking for Tcl to absolute path - use the system version
@loader_path/Tcl  ->  /System/Library/Frameworks/Tk.framework/Tk

@matysek matysek added this to the PyInstaller 3.1 milestone Sep 24, 2015

@matysek matysek self-assigned this Sep 24, 2015

@stefsmeets

This comment has been minimized.

Copy link

stefsmeets commented Sep 24, 2015

@matysek Homebrew python will use the system Tk/Tcl (version 8.5.9) that ships with OS X 10.10.

@jamb0ss

This comment has been minimized.

Copy link

jamb0ss commented Feb 2, 2016

any ideas how to fix it? I bundle my program into a single file, so I just cannot use wahid's solution... Also I've tried to install Python via homebrew - the same issue.

@wahid

This comment has been minimized.

Copy link

wahid commented Feb 2, 2016

Have you tried to manually copy the files into the app file it creates?

@jamb0ss

This comment has been minimized.

Copy link

jamb0ss commented Feb 3, 2016

Where I should put this files there? (I've tried to put them in all directories inside "app", but with no success)

@jamb0ss

This comment has been minimized.

Copy link

jamb0ss commented Feb 8, 2016

Any ideas how to bundle Tk/Tcl-based program into a single file on OS X?

@wahid

This comment has been minimized.

Copy link

wahid commented Feb 8, 2016

if it creates a *.app file I would assume you can do.

Show package contents -> Contents/MacOS or Contents/Resources and place them in one of those folders just look for the folder where there are files with the 'so' extension.

@jamb0ss

This comment has been minimized.

Copy link

jamb0ss commented Feb 8, 2016

Did it work for you? I just tried to put this files in all directories in .app file (in all at once, copy-paste) with no success, that I've mentioned before

@matysek matysek removed their assignment Feb 15, 2016

@titan-ae

This comment has been minimized.

Copy link

titan-ae commented Mar 2, 2016

I can confirm with latest pip pyinstaller, tkInter & Tcl do not package correctly on OS/X 👎

I found editing hook-_tkinter.py & pyi_rth_tkinter.py as defined here -

viotti@597af5c

Then copying manually the following files into the dist area works -

/System/Library/Frameworks/Tcl.framework/Versions/8.5/Tcl
/System/Library/Frameworks/Tk.framework/Tk

Now it runs 👍

@HarmPrinsDEX

This comment has been minimized.

Copy link

HarmPrinsDEX commented Jan 30, 2018

Hi, I had the same problem. I installed Python with Homebrew and after that installed pyinstaller, and it works like a dream. Thank you Homebrew!

@johnmlhll

This comment has been minimized.

Copy link

johnmlhll commented Feb 10, 2018

I have the same problem and posted in detail on it on stackoverflow at https://stackoverflow.com/questions/48712076/import-error-tcl-tk-modules-not-found-pyinstaller-on-mac-not-working

Any thoughts on why even .spec manual imports in the Analysis() with binaries=['/System/Library/Frameworks/Tcl.framework/Tcl'], does not work either? I tried just Tcl, then added the Tk binary.. no change... ?? Any thoughts on same?

@htgoebel

This comment has been minimized.

Copy link
Member

htgoebel commented Feb 10, 2018

There are two unfinished pull-requests for this issue: #2969 and #3132. Both make it work on OS X but break all other platform. Someone of you needs to step forward to finish these pull-requests.

@djhyman

This comment has been minimized.

Copy link

djhyman commented May 5, 2018

I am getting the following error on MacOS 10.12.6. Python 3.6.5 Pyinstaller 3.3.1. Happens on two separate machines.

Traceback (most recent call last):
  File "converterGUI.py", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PyInstaller/loader/pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "tkinter/__init__.py", line 36, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PyInstaller/loader/pyimod03_importers.py", line 714, in load_module
    module = loader.load_module(fullname)
ImportError: dlopen(/var/folders/xz/0jt0jdy12c17v45lgqd82s600000gn/T/_MEIkXoA7v/_tkinter.so, 2): Library not loaded: @loader_path/Tcl
  Referenced from: /var/folders/xz/0jt0jdy12c17v45lgqd82s600000gn/T/_MEIkXoA7v/_tkinter.so
  Reason: no suitable image found.  Did find:
	/var/folders/xz/0jt0jdy12c17v45lgqd82s600000gn/T/_MEIkXoA7v/Tcl: not a file
	/private/var/folders/xz/0jt0jdy12c17v45lgqd82s600000gn/T/_MEIkXoA7v/Tcl: not a file
[12170] Failed to execute script converterGUI
logout
@HarmPrinsDEX

This comment has been minimized.

Copy link

HarmPrinsDEX commented May 5, 2018

Hi, I had the same problem. I installed Python with Homebrew and after that installed pyinstaller with pip3, and it works like a dream.

@djhyman

This comment has been minimized.

Copy link

djhyman commented May 6, 2018

That worked for me after several hours of trying. After installing Python3 via Homebrew and then installing pyinstaller via pip3, pyinstaller myscript.py would only work with Python2.7. I finally got it working with Python3 by using python3 -m PyInstaller myscript.py . Complete pain

@saltycraig

This comment has been minimized.

Copy link
Contributor

saltycraig commented May 17, 2018

The following .spec file will get you a "folder" build (will try one-file build soon) working with Tcl/Tk 8.6 on Mac OS X 10.13.4 and Python 3.6.5. Here is my spec file contents, which I named 'osx-folder-build.spec'.

# -*- mode: python -*-

block_cipher = None

a = Analysis(['main.py'],
             pathex=['/Users/craig/projects/parou/src'],
             binaries=[('/System/Library/Frameworks/Tk.framework/Tk', 'tk'),
		       ('/System/Library/Frameworks/Tcl.framework/Tcl', 'tcl')],
             datas=[('./assets', 'assets'),
		    ('../README.md', 'docs'),
		    ('../LICENSE', 'docs'),
		    ('../CREDITS', 'docs')
	     ],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=True,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='parou',
          debug=False,
          strip=False,
          upx=True,
          console=False )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='parou')

Then run:

$ pyinstaller osx-folder-build.spec

I first did as others has done and moved Tcl & Tk binaries by hand (by writing a 'osx-build' Makefile target, actually), but adding to the binaries list worked for me.

@tanyanghan

This comment has been minimized.

Copy link

tanyanghan commented Jul 11, 2018

I had this exact problem with Mac OS X High Sierra 10.13.5 with Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 29 2018, 20:59:26). All you need to do is add the add-binary option in the pyinstaller command line:

pyinstaller --onefile \
            --add-binary='/System/Library/Frameworks/Tk.framework/Tk':'tk' \
            --add-binary='/System/Library/Frameworks/Tcl.framework/Tcl':'tcl' \
            some_app.py ```


 
@htgoebel

This comment has been minimized.

Copy link
Member

htgoebel commented Jul 11, 2018

Anybody to pick up and finish #2969?

@lalabuy948

This comment has been minimized.

Copy link

lalabuy948 commented Aug 9, 2018

I have same issue on macOS High Sierra. Python3.6.5

I tried to use --add-binary flag with path to Tcl and Tk, still doesn't work for me.

ImportError: dlopen(/var/folders/qy/9by3pxwd1077w0gffyxn2lhm0000gn/T/_MEIRFcrzs/_tkinter.cpython-36m-darwin.so, 2): Library not loaded: @loader_path/Tcl
  Referenced from: /var/folders/qy/9by3pxwd1077w0gffyxn2lhm0000gn/T/_MEIRFcrzs/_tkinter.cpython-36m-darwin.so
  Reason: no suitable image found.  Did find:
	/var/folders/qy/9by3pxwd1077w0gffyxn2lhm0000gn/T/_MEIRFcrzs/Tcl: not a file
	/var/folders/qy/9by3pxwd1077w0gffyxn2lhm0000gn/T/_MEIRFcrzs/Tcl: stat() failed with errno=22
	/private/var/folders/qy/9by3pxwd1077w0gffyxn2lhm0000gn/T/_MEIRFcrzs/Tcl: not a file
	/private/var/folders/qy/9by3pxwd1077w0gffyxn2lhm0000gn/T/_MEIRFcrzs/Tcl: stat() failed with errno=22
[15532] Failed to execute script app
@wsk170

This comment has been minimized.

Copy link

wsk170 commented Aug 13, 2018

@lalabuy948 I use macOS High Sierra, Python3.6.6. and same issue.
here is my .spec file about add binaries. and it worked. you can try it. ignore bulid warning.

binaries=[
     ('/Library/Frameworks/Python.framework/Versions/3.6/lib/libtk8.6.dylib', 'tk'),
     ('/Library/Frameworks/Python.framework/Versions/3.6/lib/libtcl8.6.dylib', 'tcl')],
@lalabuy948

This comment has been minimized.

Copy link

lalabuy948 commented Aug 14, 2018

@wsk170 Thank you for your comment. But i have absolutely same path to that binaries. And it doesnt work for me.

@ivanovaos

This comment has been minimized.

Copy link

ivanovaos commented Aug 27, 2018

Same here, providing binaries with --add-binary doesn't work. Python 3.6.3, mac os x 10.13.6

@v6

This comment has been minimized.

Copy link

v6 commented Sep 19, 2018

// , I'm getting the same issue on Python 3.7.0.

@v6

This comment has been minimized.

Copy link

v6 commented Sep 19, 2018

// , Here's an example of one of the errors:

~ $/Users/v666/proj/docxtpl/dist/tktest/tktest ; exit;
Traceback (most recent call last):
  File "site-packages/PyInstaller/loader/rthooks/pyi_rth__tkinter.py", line 28, in <module>
FileNotFoundError: Tcl data directory "/Users/v666/proj/docxtpl/dist/tktest/tcl" not found.
[73791] Failed to execute script pyi_rth__tkinter
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

[Process completed]
@v6

This comment has been minimized.

@3dp

This comment has been minimized.

Copy link

3dp commented Oct 12, 2018

Seeing this:

Traceback (most recent call last):
  File "PyInstaller/loader/rthooks/pyi_rth__tkinter.py", line 28, in <module>
FileNotFoundError: Tcl data directory "/var/folders/3p/7y007rf52kxcpg1t6rnky90wfrzr00/T/_MEIv2h6BH/tcl" not found.
[45261] Failed to execute script pyi_rth__tkinter

Been trying the various workarounds proposed with no success. Only on High Sierra (10.13.6). Windows 10 is fine.

@tazzben

This comment has been minimized.

Copy link

tazzben commented Oct 12, 2018

Seeing this:

Traceback (most recent call last):
  File "PyInstaller/loader/rthooks/pyi_rth__tkinter.py", line 28, in <module>
FileNotFoundError: Tcl data directory "/var/folders/3p/7y007rf52kxcpg1t6rnky90wfrzr00/T/_MEIv2h6BH/tcl" not found.
[45261] Failed to execute script pyi_rth__tkinter

Been trying the various workarounds proposed with no success. Only on High Sierra (10.13.6). Windows 10 is fine.

Build the directory version which will create an app bundle that you can modify. Within the bundle add empty tcl and tk directories. Then copy over the other tcl/tk directories and files (including the libs and .sh).

@3dp

This comment has been minimized.

Copy link

3dp commented Oct 13, 2018

Build the directory version which will create an app bundle that you can modify. Within the bundle add empty tcl and tk directories. Then copy over the other tcl/tk directories and files (including the libs and .sh).

Yeah... it's not as much a "workaround", though, is it? Creating an App Bundle isn't nearly as neat as a single file app, which for my purposes, is more "user friendly". I'll try the bundle method but it's not a good long term fix.

It's also extra work/effort that really shouldn't be needed.

@Hyped-247

This comment has been minimized.

Copy link

Hyped-247 commented Oct 14, 2018

After a lot of failure, I came to learn that Pyinstaller is the worst option for mac. So, if you're a mac user, then I would recommend py2app. See this url since it recommends a specific Python library based on your operating system.

@melvyn2

This comment has been minimized.

Copy link
Contributor

melvyn2 commented Oct 14, 2018

@Hyped-247 I don't find that to be true. I use PyInstaller fine for mac, it just seems to be that Tkinter isn't working too well here. Pyinstaller does work fine for many other libraries on mac.

@LevN0

This comment has been minimized.

Copy link

LevN0 commented Oct 14, 2018

There are at least 3 different solutions to the issue with-in this thread. All of them work. None of them are extremely complicated.

The easiest solution that I have personally used and thus know works for sure is this one.

@Hyped-247

This comment has been minimized.

Copy link

Hyped-247 commented Oct 14, 2018

@melvyn2 my experience with Pyinstaller has been different. Maybe there is something wrong with Tk as you wrote. And yes, Pyinstaller did work for me when I wanted to turn my python code into a terminal non-GUI program. However, when it comes to turning specifically your TK program into an executable in mac, I see that py2app is a much better option than Pyinstaller.

@3dp

This comment has been minimized.

Copy link

3dp commented Oct 14, 2018

@LevN0 - Thanks. I'll try this.

@AmbroiseGrau

This comment has been minimized.

Copy link

AmbroiseGrau commented Oct 26, 2018

Thanks. I'll try this.

@3dp Did it work for you in the end? I found an other way to go around it if needed.

@Nutshell1

This comment has been minimized.

Copy link

Nutshell1 commented Dec 9, 2018

This might be a simpler solution for all and seems to work like a charm for me. No homebrew or anaconda or copying files into folder or modifying any files needed. One file shareable app created. Works with both regular tkinter based gui or pysimplegui.

Pyinstaller instructions Mac

  1. Navigate to directory where the script/s is/are saved using terminal

  2. Copy and paste the following
    pyinstaller --onefile --add-binary='/System/Library/Frameworks/Tk.framework/Tk':'tk' --add-binary='/System/Library/Frameworks/Tcl.framework/Tcl':'tcl' xyz.py

  3. Remember to change the xyz and hit return

(Mac OS 10.11.6, python 3.7.1, pyinstaller 3.4)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment