Bob's Blog

Web开发、测试框架、自动化平台、APP开发、机器学习等

返回上页首页

解决python在Mac上tkinter导入错误



平时编程用的Mac,python是用pyenv装的多版本,当需要用到tkinter时却得到错误提示: "ModuleNotFoundError: No module named '_tkinter'"。这个就很奇怪了,因为tkinter是python内置的却无法导入。

之后查询资料发现是跟mac本身环境和pyenv有关。

解决的步骤如下:

先安装tck-tk

brew install tcl-tk

安装完成后,需要卸载当前pyenv安装的python(如果你还想用这个版本的话),或者用pyenv重新安装新版本,但需要指定参数。

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.2

建议是安装3.8之上的,我原先用的稍老的3.6.8,仍然有问题,安装较新的版本便不出现错误了。

下一篇:  Pyenv离线安装Python避免官方下载过慢
上一篇:  JS解决Selenium无法拖拽HTML5控件的问题

共有2条评论

添加评论

D change
2023年11月18日 02:30
I'm curious to find out what blog system you have been utilizing? I'm having some small security problems with my latest blog and I'd like to find something more safeguarded. Do you have any solutions?
d-change.net
2023年11月15日 22:50
Hi! I realize this is kind of off-topic however I needed to ask. Does managing a well-established website like yours take a lot of work? I am brand new to writing a blog but I do write in my journal every day. I'd like to start a blog so I can share my experience and views online. Please let me know if you have any kind of recommendations or tips for brand new aspiring bloggers. Appreciate it!