Configuring Python312 to accept tkinter and openpyxl

I upgraded from Python39 to Python312 and I have a problem running my project because of “import tkinter” and “import openpyxl” which were included in the code lines. These were working fine with Python39 and Python311 but I get errors each time I run the code with Python312. How can correct this, please?

What errors?

No module name ‘openpyxl’

other errors:
Error in sitecustomize; set PYTHONVERBOSE for traceback:
AssertionError: SRE module mismatch
Traceback (most recent call last):
File “C:\Users\Patrick Enyi\PycharmProjects\pythonProject1\main.py”, line 1, in
import tkinter as tk
File “C:\Users\Patrick Enyi\AppData\Local\Programs\Python\Python312\Lib\tkinter_init_.py”, line 38, in
import _tkinter # If this fails your Python may not be configured for Tk
^^^^^^^^^^^^^^^
ImportError: DLL load failed while importing _tkinter: The specified module could not be found.

You need to install openpyxl before you can use it.

Depending on how you installed Python, tkinter may or may not be included in the installation. In this case it seems it was not. You can add tkinter to your existing Python installation, but the method to do so depends on how you installed Python in the first place.

So, how did you install Python3.12?

I downloaded and installed Python3.12 using the Windows installation wizard and accepted the wizard’s location suggestion which differs from the location the Python3.9 was installed.
When I search for these files I find them in the subdirectories under Python3.9. tkinter is also seen in the subdirectory of Python3.12 but it is not visible during code execution

Installing openpyxl in your Python 3.12 environment should be simple enough. You will need to run pip install openpyxl, making sure to use the pip executable that belongs to Python 3.12, not 3.9.

The missing tkinter is more mysterious. It should be bundled with the Windows installer. I do not use Windows myself, but I would try repairing the installation by re-running the installer. Make sure not to select a ‘minimal installation’ or something like that; it might not include tkinter.

1 Like

Alright, I will do just that. Thanks a million.

I do, so here is one data point: it worked for me (in a virtual environment). Installation will have been a download from python.org, but I also manage my own PATH (not PYTHONPATH).

PS learn> py -m venv tkvenv
PS learn> .\tkvenv\Scripts\activate
(tkvenv) PS learn> pip install openpyxl
Collecting openpyxl ...
(tkvenv) PS learn> python
Python 3.12.1 (tags/v3.12.1:2305ca5, Dec  7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import openpyxl
>>> import tkinter

All sweet.

1 Like

I can get your error message on my installation if I hide (rename) the file Python312\DDLs\tk86t.dll, so that may be what’s missing.

Thanks Jeff. I will try your suggestion also.

You can run the Python installer again and click “Modify” to get back to this screen:

to make sure tkinter is checked, assuming you used the normal python.org installer. There’s also a “Repair” button there:

3 Likes

Thanks. I will do that tonight