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

Dzh*_*hao 1 python import namespaces tkinter python-3.x

与通过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'
Run Code Online (Sandbox Code Playgroud)

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

来自filesearch.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
Run Code Online (Sandbox Code Playgroud)

来自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'
Run Code Online (Sandbox Code Playgroud)

与这个问题不同,我只安装了一个版本的python.即,Python34.

Tad*_*sen 11

我想先说:如果您知道将使用子模块,请始终显式导入子模块.

由于结构的原因,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
Run Code Online (Sandbox Code Playgroud)

你不需要在IDLE中执行此操作的原因是,在运行代码之前,IDLE会在后台设置一些内容并最终导入一些tkinter库.其中一位维护者评论说这实际上是IDLE中的一个错误.

在python 3.6.5中(可能更早,只检查过这个版本)这个特定的差异已得到修复,在任何版本中你都可以看到加载了一些代码的子模块列表,如下所示:

# standard interpreter
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 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
Run Code Online (Sandbox Code Playgroud)

在IDLE:

Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> 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
Run Code Online (Sandbox Code Playgroud)

tkinter.constants被加载时,你只是import tkinter这样的我测试的版本,这个问题仍然存在,只urllib.parseencodings.ascii(和idlelib模块,但一般产品代码不使用)


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

>>> import pandas
>>> import http
>>> http.client
<module 'http.client' from '.../http/client.py'>
Run Code Online (Sandbox Code Playgroud)

现在假设我们写了一些仍然使用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'
Run Code Online (Sandbox Code Playgroud)

这样,当使用它的代码http.client可能通过使用恰好使用它但仍然会失败的库加载时,您可能最终得到一个正常工作的子模块.

这让我回到了我的初始点 - 总是明确导入子模块.


Sto*_*ica 5

实际上,该模块没有属性filedialog,这是一个子模块,您应该像import tkinter.filedialog使用它之前一样导入它。您可以使用tk.filedialog而无需显式导入filedialogIDLE,因为它已经被导入。

import sys
sys.modules['tkinter.filedialog']
Run Code Online (Sandbox Code Playgroud)

上面的代码将KeyError在标准python解释器中引发a ,但它将返回类似于<module 'tkinter.filedialog' from '/usr/lib/python3.5/tkinter/filedialog.py'>IDLE的内容。