gpt4 book ai didi

python - 为什么 tkinter 模块在通过命令行运行时会引发属性错误,而在通过 IDLE 运行时却不会?

转载 作者:太空宇宙 更新时间:2023-11-04 06:46:44 28 4
gpt4 key购买 nike

与通过 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 *(我提到的代码行位于 filesearch 文件中。)

我通过 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。

最佳答案

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

由于 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 个模块之外的所有模块中。

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

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

在空闲状态下:

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 时,您最终可能会得到一个正常工作的子模块,可能是通过使用碰巧使用它但会失败的库。

这让我回到了最初的观点——总是明确地导入子模块。

关于python - 为什么 tkinter 模块在通过命令行运行时会引发属性错误,而在通过 IDLE 运行时却不会?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36163561/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com