Skip to content

Tkinter - Can't find a usable init.tcl #111754

Closed as not planned
Closed as not planned

Description

@oldium

Bug report

Bug description:

I tried to add widgetPlus from Tklib and its dependency snit from Tcllib to my project, but executing root.tk.call("package", "require", "widgetPlus") after instantiating root = tkinter.Tk() failed with Tcl error:

_tkinter.TclError: Can't find a usable init.tcl in the following directories:
    C:/Users/oldium/AppData/Local/Programs/Python/Python312/lib/tcl8.6 C:/Users/oldium/AppData/Local/Programs/Python/lib/tcl8.6 C:/Users/oldium/AppData/Local/Programs/lib/tcl8.6 C:/Users/oldium/AppData/Local/Programs/Python/library C:/Users/oldium/AppData/Local/Programs/library C:/Users/oldium/AppData/Local/Programs/tcl8.6.13/library C:/Users/oldium/AppData/Local/tcl8.6.13/library

The real TCL installation path is C:/Users/oldium/AppData/Local/Programs/Python/Python312/tcl/tcl8.6, note tcl instead of lib.

Strangely, the same application worked when started as exe after PyInstaller was used. I tracked the problem down to the minimal test case (as used by Tcllib module snit):

import glob
import os
import sys
import tkinter as tk
from pathlib import Path

if __name__ == "__main__":
    tcl = tk.Tcl()
    interpreter = tcl.call("interp", "create")
    tcl.call("interp", "delete", interpreter)
    tcl.call("puts", "It worked!")

I also found a workaround (I used glob to prevent fixing on a particular Tcl/Tk version), which works both in “normal” and venv environments:

# Fix Tcl/Tk folder
os.environ["TCL_LIBRARY"] = str(Path(glob.glob(os.path.join(sys.base_prefix, "tcl", "tcl*", "init.tcl"))[0]).parent)
os.environ["TK_LIBRARY"] = str(Path(glob.glob(os.path.join(sys.base_prefix, "tcl", "tk*", "pkgIndex.tcl"))[0]).parent)
os.environ["TIX_LIBRARY"] = str(Path(glob.glob(os.path.join(sys.base_prefix, "tcl", "tix*", "pkgIndex.tcl"))[0]).parent)

It looks like the Tcl/Tk installation works fine but needs additional configuration on the Python side. And please note that the workaround is usable for the Python's Tcl/Tk installation and does not consider existing Tcl/Tk installations and existing *_LIBRARY environmental variables.

CPython versions tested on:

3.11, 3.12

Operating systems tested on:

Windows

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Nov 5, 2023
  2. oldium commented on Nov 5, 2023

    @oldium
    Author

    This works in PyInstaller's generated exe because it sets the TCL_LIBRARY and TK_LIBRARY by itself as seen in Process Explorer:

    image

    Maybe Python could do the same as PyInstaller does, i.e. set TCL_LIBRARY and TK_LIBRARY (and TIX_LIBRARY?) if they are not already set?

  3. added a commit that references this issue on May 5, 2024
  4. FujiwaraChoki commented on Nov 26, 2024

    @FujiwaraChoki

    brew install tcl-tk fixed it for me.

  5. oldium commented on Nov 26, 2024

    @oldium
    Author

    brew install tcl-tk fixed it for me.

    On Windows? 🤔

    Tcl/Tk is part of Python installer on Windows already.

    Edit: You are talking about separate installation of Tcl/Tk, so you probably already have the environment variables set correctly, but I am talking about embedded version of Tcl/Tk installed by the Python installer on Windows, which resides in the Python installation directory.

  6. FujiwaraChoki commented on Nov 27, 2024

    @FujiwaraChoki

    brew install tcl-tk fixed it for me.

    On Windows? 🤔

    Tcl/Tk is part of Python installer on Windows already.

    Edit: You are talking about separate installation of Tcl/Tk, so you probably already have the environment variables set correctly, but I am talking about embedded version of Tcl/Tk installed by the Python installer on Windows, which resides in the Python installation directory.

    Yeah I'm on an M3 Mac, so not Windows. The issue is likely that there is a discrepancy between the Python and TCL/TK Versions. You need to re-install (re-build) Python everytime you install another version of TCL/TK.

    I do not know the solution for windows.

  7. oldium commented on Nov 27, 2024

    @oldium
    Author

    brew install tcl-tk fixed it for me.

    On Windows? 🤔
    Tcl/Tk is part of Python installer on Windows already.
    Edit: You are talking about separate installation of Tcl/Tk, so you probably already have the environment variables set correctly, but I am talking about embedded version of Tcl/Tk installed by the Python installer on Windows, which resides in the Python installation directory.

    Yeah I'm on an M3 Mac, so not Windows. The issue is likely that there is a discrepancy between the Python and TCL/TK Versions. You need to re-install (re-build) Python everytime you install another version of TCL/TK.

    There is no discrepancy in versions. The Tcl/Tk runtime simply cannot find the installation path, because it expects it to be in <Python Dir>/lib/tcl8.6 (some auto-detection), but it is actually stored in <Python Dir>/tcl/tcl8.6. So the Tcl autodiscovery does not find it.

    I do not know the solution for windows.

    I already mentioned possible solution/workaround in the issue description - set the environment variables TCL_LIBRARY/TK_LIBRARY/TIX_LIBRARY by Python if they are not already set.

  8. FujiwaraChoki commented on Nov 27, 2024

    @FujiwaraChoki

    brew install tcl-tk fixed it for me.

    On Windows? 🤔
    Tcl/Tk is part of Python installer on Windows already.
    Edit: You are talking about separate installation of Tcl/Tk, so you probably already have the environment variables set correctly, but I am talking about embedded version of Tcl/Tk installed by the Python installer on Windows, which resides in the Python installation directory.

    Yeah I'm on an M3 Mac, so not Windows. The issue is likely that there is a discrepancy between the Python and TCL/TK Versions. You need to re-install (re-build) Python everytime you install another version of TCL/TK.

    There is no discrepancy in versions. The Tcl/Tk runtime simply cannot find the installation path, because it expects it to be in <Python Dir>/lib/tcl8.6 (some auto-detection), but it is actually stored in <Python Dir>/tcl/tcl8.6. So the Tcl autodiscovery does not find it.

    I do not know the solution for windows.

    I already mentioned possible solution/workaround in the issue description - set the environment variables TCL_LIBRARY/TK_LIBRARY/TIX_LIBRARY by Python if they are not already set.

    I know it's because of the paths.. The path had the wrong version in it which is why it wasn't working for me. Also I'm not on windows.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions