python idle运行时显示3错误_python – 为什么tkinter模块在通过命令行运行时引发属性错误但在通过IDLE运行时不会引发属性错误?...

与通过IDLE的运行模块f5命令运行时相比,通过命令行运行时代码是否会引发错误?

最近我一直在努力提高代码的可读性和健壮性.因此,我一直在尝试删除所有模块导入*行.我以前使用tkinter import *,我的代码行完全正常:

self.path = filedialog.askdirectory()

但是现在我已经从tkinter import *更改为导入tkinter作为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'

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

来自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

来自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个模块之外,它不再发生.

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

# 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

在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

刚刚导入tkinter时加载了tkinter.constants,因为我测试的版本只有urllib.parse和encodings.ascii(和idlelib模块,但通常生产代码不使用)

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

>>> import pandas

>>> import http

>>> http.client

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

>>> import http

>>> http.client

Traceback (most recent call last):

File "", line 1, in

AttributeError: module 'http' has no attribute 'client'

这样,当使用它的代码可能通过使用碰巧使用它的库加载http.client但是否会失败时,你可能会得到一个正常工作的子模块.

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值