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