【问题标题】:Why tkinter module raises attribute error when run via command line but not when run via IDLE?为什么 tkinter 模块在通过命令行运行时会引发属性错误,但在通过 IDLE 运行时不会?
【发布时间】:2016-07-09 21:09:29
【问题描述】:

与通过 IDLE 的 run module f5 命令运行相比,通过命令行运行代码会引发错误是否有原因?

最近我一直在努力提高我的代码的可读性和健壮性。结果,我一直在尝试删除所有 from module import * 行。我曾经使用from tkinter import *,我的这行代码运行良好:

self.path = filedialog.askdirectory()

但现在我已将from tkinter import * 更改为import tkinter as tk 并相应地更改了代码:

self.path = tk.filedialog.askdirectory()

一个名为 GUI.py 的文件使用以下命令导入此文件:from lib.filesearch import *(我提到的代码行位于文件搜索文件中。)

我通过 IDLE 运行我的代码,一切都很好。我的 GUI 仍然可以工作,self.path = tk.filedialog.askdirectory() 行正常工作但是,当我通过 windows 命令行运行代码时,我得到了错误:

AttributeError: 'module' object has no attribute 'filedialog'

以下是我的代码中的相关位:

来自文件搜索.py

import tkinter as tk
    def get_path(self):
        """Store user chosen path to search"""
        self.paths = tk.filedialog.askdirectory(initialdir = FileSearch.DEFAULT)
        return self.paths

来自 GUI.py

from lib.filesearch import *    
    def Browse(self):
        self.BrowseB['state']='disabled'
        self.p=self.CrawlObj.get_path()
        self.AddText('Searching from Path: ' + str(self.p))
        self.BrowseB['state']='normal'

question 不同,我只安装了一个版本的 python。即 Python34。

【问题讨论】:

  • 尝试添加一个简单的调试print(tk) 以查看它实际导入的内容。最初会想到不同的 python 路径。
  • @Ilja 来自 IDLE:<module 'tkinter' from 'C:\\Python34\\lib\\tkinter\\__init__.py'> 和来自命令行:<module 'tkinter' from 'C:\\Python34\\lib\\tkinter\\__init__.py'>
  • 你需要导入带有import tkinter.filedialog的子模块来加载它,你没有在IDLE中的原因是因为idle已经导入了它,因为它在tkinter上运行
  • 看看the docs关于在他们的__init__.py中定义__all__属性的包,它定义了在导入顶级模块时要加载哪些子模块,一些包像numpymatplotlib 导入 lots 个子模块,这就是为什么它们需要很长时间才能加载,但其他包(如 tkinterPIL)只会加载您明确要求它们加载的包。
  • 这在某种意义上是IDLE中的一个bug,它的内部操作以这种方式暴露出来。我希望有一天通过重构几个模块来解决这个问题,这样在用户代码运行之前甚至不会将 tkinter 导入用户进程。

标签: python python-3.x import tkinter namespaces


【解决方案1】:

首先我想说:如果您知道会使用子模块,请始终显式导入它们。这个答案的结尾有一个更令人信服的案例,这很重要。

由于tkinter 的结构,您必须显式导入子模块才能加载:

import tkinter as tk
print(hasattr(tk,"filedialog")) # in a standard interpreter will print false
import tkinter.filedialog
print(hasattr(tk,"filedialog")) # should always print true after explicit import

您不需要在 IDLE 中执行此操作的原因是,在您的代码运行之前,IDLE 在后台设置了一些东西并最终导入了一些 tkinter 库。一位维护者has commented 说这实际上是 IDLE 中的一个错误。

在 python 3.6.5 中(可能更早,只检查了这个版本)这个特定的差异已经修复所以它不再发生在我下面显示的 2 个模块之外。 em>

在任何版本中,您都可以看到加载了如下代码的子模块列表:

Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55)
# standard interpreter 
>>> import sys
>>> len(sys.modules) #total number of modules automatically loaded
71
>>> sorted(name for name in sys.modules.keys() if ("." in name)) #submodules loaded
['collections.abc', 'encodings.aliases', 'encodings.latin_1', 'encodings.utf_8', 'importlib._bootstrap', 'importlib._bootstrap_external', 'importlib.abc', 'importlib.machinery', 'importlib.util', 'os.path']
>>> len(_) #number of submodules
10

在 IDLE 中:

Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55) 
# IDLE
>>> import sys
>>> len(sys.modules)
152
>>> sorted(name for name in sys.modules.keys() if ("." in name and "idlelib" not in name))
['collections.abc', 'encodings.aliases', 'encodings.ascii', 'encodings.latin_1', 'encodings.utf_8', 'importlib._bootstrap', 'importlib._bootstrap_external', 'importlib.abc', 'importlib.machinery', 'importlib.util', 'os.path', 'tkinter.constants', 'urllib.parse']
>>> len(_) #number of submodules not directly related to idlelib.
13

tkinter.constants 仅在 import tkinter 时加载,因此在我测试的版本中,此问题仅存在于 urllib.parseencodings.ascii(以及 idlelib 模块,但通常生产代码不使用它)


这不一定是 IDLE 特定的问题,更糟糕的问题是子模块是否由您使用的另一个库加载。以如下代码为例:

>>> import pandas
>>> import http
>>> http.client
<module 'http.client' from '.../http/client.py'>

现在假设我们编写了一些其他代码,仍然使用 http.client 但没有使用 pandas:

>>> import http
>>> http.client
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'http' has no attribute 'client'

通过这种方式,当使用它的代码加载 http.client 时,您可能会得到一个可以正常工作的子模块,可能是通过使用恰好使用它但会失败的库。

这将我带回到我的初始点 - 始终显式导入子模块。

【讨论】:

    【解决方案2】:

    其实这个模块确实没有filedialog属性,它是一个子模块,你应该在使用它之前将它导入为import tkinter.filedialog。您可以使用 tk.filedialog 而无需在 IDLE 中显式导入 filedialog,因为它已经导入。

    import sys
    sys.modules['tkinter.filedialog']
    

    上面的代码会在标准的 Python 解释器中引发 KeyError,但它会在 IDLE 中返回类似于 &lt;module 'tkinter.filedialog' from '/usr/lib/python3.5/tkinter/filedialog.py'&gt; 的内容。

    【讨论】:

      猜你喜欢
      • 2015-05-27
      • 2012-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多