Car*_*l G 42 python macos tkinter pyenv
通过 pyenv 安装的 Python 版本无法导入tkinter:
? python
Python 3.8.1 (default, Feb 29 2020, 11:45:59)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/factor/.pyenv/versions/3.8.1/lib/python3.8/tkinter/__init__.py", line 36, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
>>>
Run Code Online (Sandbox Code Playgroud)
或者您可能会收到有关标头版本与二进制文件不匹配的消息:
DEPRECATION WARNING: The system version of Tk is deprecated and may be removed in a future release. Please don't rely on it. Set TK_SILENCE_DEPRECATION=1 to suppress this warning.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/factor/.pyenv/versions/3.8.1/lib/python3.8/tkinter/__init__.py", line 4552, in _test
root = Tk()
File "/Users/factor/.pyenv/versions/3.8.1/lib/python3.8/tkinter/__init__.py", line 2263, in __init__
self._loadtk()
File "/Users/factor/.pyenv/versions/3.8.1/lib/python3.8/tkinter/__init__.py", line 2279, in _loadtk
raise RuntimeError("tk.h version (%s) doesn't match libtk.a version (%s)"
RuntimeError: tk.h version (8.6) doesn't match libtk.a version (8.5)
Run Code Online (Sandbox Code Playgroud)
Car*_*l G 82
TL;DR 设置环境。变量。中提到tcl-tk的注意事项和 本GitHub的评论通过pyenv获得安装新的蟒蛇时tkinter。
首先,确保你有最新的tcl-tk自制软件,然后注意它的警告:
? brew install tcl-tk
? brew info tcl-tk
tcl-tk: stable 8.6.10 (bottled) [keg-only]
...
==> Caveats
tcl-tk is keg-only, which means it was not symlinked into /usr/local,
because tk installs some X11 headers and macOS provides an (older) Tcl/Tk.
If you need to have tcl-tk first in your PATH run:
echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.zshrc
For compilers to find tcl-tk you may need to set:
export LDFLAGS="-L/usr/local/opt/tcl-tk/lib"
export CPPFLAGS="-I/usr/local/opt/tcl-tk/include"
For pkg-config to find tcl-tk you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/tcl-tk/lib/pkgconfig"
...
Run Code Online (Sandbox Code Playgroud)
您还需要了解 pyenv 的PYTHON_CONFIGURE_OPTS、--with-tcltk-includes、 和--with-tcltk-libs,例如从此评论中。
接下来,在环境变量处于活动状态的情况下重新安装 Python:
? pyenv uninstall 3.8.1
? env \
PATH="$(brew --prefix tcl-tk)/bin:$PATH" \
LDFLAGS="-L$(brew --prefix tcl-tk)/lib" \
CPPFLAGS="-I$(brew --prefix tcl-tk)/include" \
PKG_CONFIG_PATH="$(brew --prefix tcl-tk)/lib/pkgconfig" \
CFLAGS="-I$(brew --prefix tcl-tk)/include" \
PYTHON_CONFIGURE_OPTS="--with-tcltk-includes='-I$(brew --prefix tcl-tk)/include' --with-tcltk-libs='-L$(brew --prefix tcl-tk)/lib -ltcl8.6 -ltk8.6'" \
pyenv install 3.8.1
Run Code Online (Sandbox Code Playgroud)
它现在应该可以工作:
? pyenv global 3.8.1
? python
Python 3.8.1 (default, Feb 29 2020, 11:56:10)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> tkinter.TclVersion, tkinter.TkVersion
(8.6, 8.6)
>>> tkinter._test()
# You should get a GUI
Run Code Online (Sandbox Code Playgroud)
如果您收到以下错误,则您可能缺少PYTHON_CONFIGURE_OPTSenv。变种 以上。
DEPRECATION WARNING: The system version of Tk is deprecated and may be removed in a future release. Please don't rely on it. Set TK_SILENCE_DEPRECATION=1 to suppress this warning.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/factor/.pyenv/versions/3.8.1/lib/python3.8/tkinter/__init__.py", line 4552, in _test
root = Tk()
File "/Users/factor/.pyenv/versions/3.8.1/lib/python3.8/tkinter/__init__.py", line 2263, in __init__
self._loadtk()
File "/Users/factor/.pyenv/versions/3.8.1/lib/python3.8/tkinter/__init__.py", line 2279, in _loadtk
raise RuntimeError("tk.h version (%s) doesn't match libtk.a version (%s)"
RuntimeError: tk.h version (8.6) doesn't match libtk.a version (8.5)
Run Code Online (Sandbox Code Playgroud)
Dad*_*dyM 27
如果您在 macOS 上用于 Python 环境管理,这里是使(和空闲)工作的分步指南:tkinterpyenv
tcl-tk用 Homebrew安装。在 shell 中运行brew install tcl-tkecho 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.zshrcTerminal应用程序或运行重新加载外壳source ~/.zshrctck-tk在$PATH. 运行echo $PATH | grep --color=auto tcl-tk。结果,您应该看到tcl-tk突出显示的$PATH 内容export LDFLAGS="-L/usr/local/opt/tcl-tk/lib"export CPPFLAGS="-I/usr/local/opt/tcl-tk/include"export PKG_CONFIG_PATH="/usr/local/opt/tcl-tk/lib/pkgconfig"pyenv则使用pyenv uninstall <your python version>. 例如pyenv uninstall 3.8.2python-build。在 shell 中运行PYTHON_CONFIGURE_OPTS="--with-tcltk-includes='-I/usr/local/opt/tcl-tk/include' --with-tcltk-libs='-L/usr/local/opt/tcl-tk/lib -ltcl8.6 -ltk8.6'"
注意:将来使用tck-tk实际安装 Homebrew 的版本。在发布的那一刻8.6是实际的pyenv用pyenv install <version>. 例如pyenv install 3.8.2测试
pyenv global <verion that you've just installed>idle. 您应该会看到没有任何警告的空闲窗口和“以红色打印的文本”。tkinter。在 shell 中运行python -m tkinter -c "tkinter._test()". 您应该会看到如图所示的测试窗口:就是这样!
我的环境:
检查这是不是在执行上述步骤时出错:
zsh(包含在 macOS Catalina 中)=上面的“shell”pyenv(根据pyenvGitHub 的官方自述文件安装了 Homebrew 和 PATH 更新)3.8.x- 3.9.x(用pyenv install <version>命令安装)小智 11
在撰写本文时,它似乎tcl-tk不再仅限于 keg,因此不再需要设置环境变量:
\xe2\x80\xbb brew info tcl-tk\n==> tcl-tk: stable 8.6.13 (bottled)\n...\n==> Caveats\nThe sqlite3_analyzer binary is in the `sqlite-analyzer` formula.\n==> Analytics\n...\nRun Code Online (Sandbox Code Playgroud)\n事实上,我只需运行以下命令即可提供pyenv并tkinter解决海报的问题:
\xe2\x80\xbb pyenv uninstall <python version>\n\xe2\x80\xbb brew install tcl-tk\n\xe2\x80\xbb pyenv install <python version>\nRun Code Online (Sandbox Code Playgroud)\n(FWIW,我在 Apple 硅处理器上使用 MacOS Ventura。)
\n| 归档时间: |
|
| 查看次数: |
11981 次 |
| 最近记录: |