简体   繁体   中英

install tkinter on mac os and pyenv

When I import tkinter, I get this error:

No module named '_tkinter'

I used both the installation here: https://platform.activestate.com/oferlip/ActiveTcl-8.5/distributions

and also tried this command:

brew install tcl-tk

I saw in this github comment: https://github.com/pyenv/pyenv/issues/1375#issuecomment-533182043

which states that I need to uninstall and then reinstall the pyenv (after installing tck-tk).

I have a lot of installation on my pyenv, is there a way to use tkinter without uninstalling my virtualenv?

I looked it up, and I am pretty sure Tkinter comes pre-installed with Python

Try:

    import tkinter as tk
    from tkinter import *
    print("It works!")

Good luck!

brew install python-tk@3.9

see this formula

Using tkinter with pyenv can be tricky due to missing dependencies.

The following two steps should help you fix the tkinter and pyenv issue.

Step 1: Installing Necessary System Packages

Using tkinter with pyenv can be tricky due to missing dependencies.

First, let's make sure we have installed the necessary system packages for tkinter .

On macOS, you can use Homebrew:

brew install tcl-tk

Step 2: Linking the Correct Tcl/Tk Versions

Next, make sure you link the correct versions of Tcl/Tk when installing Python. pyenv builds Python in your environment, but if you don't have the required dependencies, like the Tk/Tcl libraries, it'll build Python without them.

If the Python version you want to use is already installed on your system, you'll need to uninstall it before proceeding. For example:

pyenv uninstall 3.11.3

When installing a new Python version with pyenv , use the following commands:

For macOS, after installing tcl-tk with brew:

brew install openssl readline sqlite3 xz zlib

env LDFLAGS="-L$(brew --prefix openssl@1.1)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix sqlite3)/lib -L$(brew --prefix xz)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix tcl-tk)/lib" \
CPPFLAGS="-I$(brew --prefix openssl@1.1)/include -I$(brew --prefix readline)/include -I$(brew --prefix sqlite3)/include -I$(brew --prefix xz)/include -I$(brew --prefix zlib)/include -I$(brew --prefix tcl-tk)/include" \
PKG_CONFIG_PATH="$(brew --prefix openssl@1.1)/lib/pkgconfig:$(brew --prefix readline)/lib/pkgconfig:$(brew --prefix sqlite3)/lib/pkgconfig:$(brew --prefix xz)/lib/pkgconfig:$(brew --prefix zlib)/lib/pkgconfig:$(brew --prefix tcl-tk)/lib/pkgconfig" \
pyenv install <version>

Replace <version> with the version of Python you want to install. After that, you should be able to import tkinter in your pyenv Python environment.

Note that I couldn't get tkinter to work with pyenv on Python 3.11.0 , but it worked perfectly on 3.11.2 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM