Skip to content
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

win 10 Python-3.13.0 tkinter fails in venv #125235

Closed
replabrobin opened this issue Oct 10, 2024 · 11 comments
Closed

win 10 Python-3.13.0 tkinter fails in venv #125235

replabrobin opened this issue Oct 10, 2024 · 11 comments
Labels
OS-windows type-bug An unexpected behavior, bug, or error

Comments

@replabrobin
Copy link

replabrobin commented Oct 10, 2024

Bug report

Bug description:

Installed python 3.13.0 in windows 10 at c:\python313

created venv at c:\users\rptla\tmp\py313

Tkinter based app failed to start in the venv. After testing tkinter I see these results ie error in venv starting Tk() wotks in installed.

In the venv

(py313) C:\Users\rptla\tmp\py313>python
Python 3.13.0 (tags/v3.13.0:60403a5, Oct  7 2024, 09:38:07) [MSC v.1941 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> root = tkinter.Tk()
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    root = tkinter.Tk()
  File "C:\python313\Lib\tkinter\__init__.py", line 2459, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
              ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_tkinter.TclError: Can't find a usable init.tcl in the following directories:
    C:/python313/lib/tcl8.6 C:/lib/tcl8.6 C:/lib/tcl8.6 C:/library C:/library C:/tcl8.6.14/library C:/tcl8.6.14/library



This probably means that Tcl wasn't installed properly.

>>>

in installed python

C:\Python313\Lib\tkinter>\python313\python
Python 3.13.0 (tags/v3.13.0:60403a5, Oct  7 2024, 09:38:07) [MSC v.1941 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> root=tkinter.Tk()
>>>

CPython versions tested on:

3.13

Operating systems tested on:

Windows

Linked PRs

@replabrobin replabrobin added the type-bug An unexpected behavior, bug, or error label Oct 10, 2024
@replabrobin
Copy link
Author

replabrobin commented Oct 10, 2024

FWIW i found that setting TCL_LIBRARY to point at the installation\tcl\tcl8.6 prevents Tk() from raising an exception.

However, running my app leaves a command window visible which it should not. I tried running the app directly with python w ie

pythonw -mmodule.module.app

that still leaves the command window visible.

I tried various settings for TK_LIBRARY, but that dis not help.

@y5c4l3
Copy link
Contributor

y5c4l3 commented Oct 10, 2024

Behaviors of TCL paths on Windows are sort of different, they look it up at sys.prefix which could be a venv root. PR changes it to sys.base_prefix.

PyObject *prefix = PySys_GetObject("prefix"); // borrowed reference

@RicardoEscobar
Copy link

My guess is that this requieres a 3.13.1 patch to fix it.

@joshburnett
Copy link

I see the same failure in Windows 11 as well, running in a venv:

Python 3.13.0 (tags/v3.13.0:60403a5, Oct  7 2024, 09:38:07) [MSC v.1941 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> root = tkinter.Tk()
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    root = tkinter.Tk()
  File "C:\Users\jburnett1\AppData\Local\Programs\Python\Python313\Lib\tkinter\__init__.py", line 2459, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
              ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_tkinter.TclError: Can't find a usable init.tcl in the following directories:
    C:/Users/MY_USER_NAME/AppData/Local/Programs/Python/Python313/lib/tcl8.6 C:/Users/jburnett1/AppData/Local/Programs/Python/lib/tcl8.6 C:/Users/MY_USER_NAMEAppData/Local/Programs/lib/tcl8.6 C:/Users/MY_USER_NAME/AppData/Local/Programs/Python/library C:/Users/MY_USER_NAME/AppData/Local/Programs/library C:/Users/MY_USER_NAME/AppData/Local/Programs/tcl8.6.14/library C:/Users/MY_USER_NAME/AppData/Local/tcl8.6.14/library

Running in the base Python install works OK.

My python.org-installed version is installed for just my user, and I do see the above-mentioned init.tcl file in C:\Users\MY_USER_NAME\AppData\Local\Programs\Python\Python313\tcl\tcl8.6 but this directory is not listed in the traceback as one of the ones being searched.

@y5c4l3
Copy link
Contributor

y5c4l3 commented Oct 11, 2024

Bisects to c67121a, cc @vstinner would you mind having a look at my PR? :-)

vstinner pushed a commit that referenced this issue Oct 11, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 11, 2024
…ion on Windows (pythonGH-125250)

(cherry picked from commit b3aa1b5)

Co-authored-by: Y5 <[email protected]>
Signed-off-by: y5c4l3 <[email protected]>
@vstinner
Copy link
Member

On Windows, when I build Python from source, TCL is loaded from externals\tcltk-8.6.15.0 directory using an absolute path, so the bug was not spotted before. Even with a venv, externals\tcltk-8.6.15.0 is found and used.

Thank you for the bug report and the fix.

vstinner pushed a commit that referenced this issue Oct 11, 2024
…tion on Windows (GH-125250) (#125312)

gh-125235: Keep `_tkinter` TCL paths pointing to base installation on Windows (GH-125250)
(cherry picked from commit b3aa1b5)

Signed-off-by: y5c4l3 <[email protected]>
Co-authored-by: Y5 <[email protected]>
@GnikSuc
Copy link

GnikSuc commented Oct 15, 2024

I have this same issue on win11. I also tried to add tcl and tk folders from source into \venv\Lib\site-packages, but it had no effect.

@vstinner
Copy link
Member

I have this same issue on win11. I also tried to add tcl and tk folders from source into \venv\Lib\site-packages, but it had no effect.

You should wait for Python 3.13.1 release to get the fix. You can use Python 3.12 in the meanwhile.

Stanley5249 added a commit to Stanley5249/stack-literal-eval that referenced this issue Oct 19, 2024
This reverts commit 2fd2b3f because
Python 3.13 has a tkinter bug. See details at
python/cpython#125235.
@Bran-Mak-Morn
Copy link

I can confirm the above - 3.13.0 Python Tcl wasn't installed properly (I'm using W11, Pycharm, venv). So you either provide paths for your Tkinter projects and keep 3.13.0 - an example below:

import os
os.environ['TCL_LIBRARY'] = r'path......'
os.environ['TK_LIBRARY'] = r'path......'

or go back to 3.12. :)

@bersbersbers
Copy link

I have used @replabrobin's workaround (setting environment variables) to mitigate this issue to a large degree.

Unfortunately, I have also noticed that this approach

still leaves the command window visible.

For the record, I believe that is a separate issue, and I just came across #126084 which seems to take care of it.

@bvermeulen
Copy link

Until fixed add the following to your environment variables:

TCL_LIBRARY = C:\Users\<user>\AppData\Local\Programs\Python\Python313\tcl\tcl8.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OS-windows type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

10 participants