TransWikia.com

Solution to Python3/tkinter import error "Symbol not found: __cg_jpeg_resync_to_restart"

Stack Overflow Asked by user405 on November 14, 2021

I was GOING to ask for help with this annoying problem, but I have solved it and want to document the stupid solution for the next person who searches SO for the string __cg_jpeg_resync_to_restart and gets (literally) 47 hits.

Here’s the problem: on attempting to use tkinter for the first time with Python3, the following error may occur when you import tkinter:

ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/Current/lib/python3.8/lib-dynload/_tkinter.cpython-38-darwin.so, 2): Symbol not found: __cg_jpeg_resync_to_restart
  Referenced from: /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
  Expected in: /usr/local/lib/libJPEG.dylib
 in /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO

Python doc is clear that 3.7+ contain their own copy of tkinter, and in fact the first line above confirms that. So the issue isn’t a wrong version of tkinter or some other tkinter install issue. But tkinter is trying to link to the MacOS copy of ImagIO, and that is trying to dynamically link what should be a perfectly standard thing, libJPEG.dylib. However that lib apparently doesn’t define an entry point __cg_jpeg_resync_to_restart that ImagIO expects.

Now what?

One Answer

This sounds like a version mismatch, ImageIO is getting a JPEG lib of a different version than it expects. But why? I check using homebrew, I have the very latest jpeg (9), it is in /usr/local/lib/libjpeg.dylib. That's odd, the name is all lowercase? I tell homebrew to uninstall its version of the jpeg cask, and it will not do that because libjpeg is a prereq of ghostscript, imagemagick, and about 6 other things brew has installed.

Most of the SO hits on __cg_jpeg_resync_to_restart talk about re-aiming DYLD_LIBRARY_PATH various ways and in fact that is the solution. After over 3 hours of futzing around, trying to find other versions of libjpeg, I finally look in the one place that is called out in the message(!),

$ find /System/Library/Frameworks/ImageIO.framework -name '*.dylib'
/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libRadiance.dylib
/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libJP2.dylib
/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libPng.dylib
/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libGIF.dylib
/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libOpenEXR.dylib
/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libJPEG.dylib
/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libTIFF.dylib

Bingo. Look, it even uses capital letter JPEG in the name, as in the message! The ImageIO distributed with MacOS has its own private collection of libs, and when I set DYLD_LIBRARY_PATH for Python's use, I confused it. Adding /System/Library/Frameworks/ImageIO.framework/Versions/A/Resources at the head of DYLD_LIBRARY_PATH before starting Python3 and tkinter imports fine!

It remains to be seen if other modules that expect libjpeg.dylib version 9 are going to barf now. Or will they ignore libJPEG and go on to /usr/local/libjpeg? I would appreciate comments on that and any issues here.

Answered by user405 on November 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP