使用 Python 3 在 Mac 上没有名为“_tkinter"的模块 [英] No module named '_tkinter' on Mac using Python 3

查看:28
本文介绍了使用 Python 3 在 Mac 上没有名为“_tkinter"的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Tkinter 作为 GUI 将我的脚本捆绑到 .app 中,并且进行捆绑的包装器是 Platypus.

I'm trying to bundle my script into a .app using Tkinter as the GUI and the wrapper that does the bundling is Platypus.

我在运行应用程序时收到此错误.

I'm getting this error when I go to run the app.

Traceback (most recent call last):
File "/Users/samb/Desktop/Beta1.app/Contents/Resources/script", line 4, in <module>
  import tkinter
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/P...ework/Versions/3.7/lib/python3.7/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'

我在这台机器上运行 Python 3.7.4,我的脚本使用:

I'm running Python 3.7.4 on this machine, and my script uses:

import tkinter
from tkinter import filedialog

这应该使它兼容.该脚本在我使用终端运行时有效,但在我运行应用程序时无效.

which should make it compatible. The script works when I run it using terminal, just not when I run the app.

我在捆绑应用程序中指定它也应该使用 python3...

I've specified in the bundling app that it should use python3 as well...

不确定我在这里做错了什么?

Not sure what I'm doing wrong here?

推荐答案

几个问题: /usr/bin/python3 中没有文件.使用 /usr/local/bin/python3/usr/bin/envpython3 作为参数.或者您的特定 Python 环境需要什么.

Several problems: There is no file at /usr/bin/python3. Either use /usr/local/bin/python3 or /usr/bin/env with python3 as args. Or whatever your particular python environment needs.

出于某种原因,python 正在寻找 Xcode 内部的库,这是不寻常的.我已经安装了 python3 并且在

For some reason, python is looking for the library inside Xcode, which is unusual. I have python3 installed and there's no python in

/Applications/Xcode.app/Contents/Developer/Library/Frameworks/

在我的安装中,有效的文件路径是:

On my installation, the valid filepath is:

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py

(即在/Library 中,而不是在 Xcode 中.)由于脚本在终端中工作,因此鸭嘴兽的配置很可能在某种程度上是错误的.

(i.e. in /Library, not inside Xcode.) As the script works in Terminal, it's likely that Platypus's config is wrong somehow.

作为一个快速的技巧,我想您可以将 Python.framework 从/Library/Frameworks 符号链接到 Xcode 内的 Frameworks 文件夹.

As a quick hack, I suppose you could symlink Python.framework from /Library/Frameworks to the Frameworks folder inside Xcode.

如评论中所述,您不需要包含第一行 import.目前,您要导入所有 tkinter,然后只导入 filedialog.from 的重点是避免导入整个库.

As stated in the comments, you don't need to include the first import line. Currently, you're importing all of tkinter, and then importing just the filedialog. The whole point of from is to avoid importing the entire library.

这篇关于使用 Python 3 在 Mac 上没有名为“_tkinter"的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆