Importing from a Package in IDLE vs Shell

泄露秘密 提交于 2019-11-27 08:37:28

问题


Importing a whole package works in IDLE, but not in shell. The following works fine in IDLE:

import tkinter as tk
tk.filedialog.askopenfilename()

In shell, I get this error:

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

I understand that I have to import tkinter.filedialog to make this work in shell.

Why the difference between IDLE and shell? How can I make IDLE act like shell? It can be frustrating to have a script working in IDLE, and failing in shell.

I am using Python 3.4.


回答1:


This is an IDLE bug which I fixed for future 3.5.3 and 3.6.0a4 releases. Tracker issue.

For an existing 3.5 or 3.4 release, add the following to idlelib/run.py just before the LOCALHOST line.

for mod in ('simpledialog', 'messagebox', 'font',
            'dialog', 'filedialog', 'commondialog',
            'colorchooser'):
    delattr(tkinter, mod)
    del sys.modules['tkinter.' + mod]

I presume that this will work with earlier 3.x releases, but do not have them installed to test. For existing 3.6.0a_ releases, replace 'colorchooser' with 'ttk'.



来源:https://stackoverflow.com/questions/38806673/importing-from-a-package-in-idle-vs-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!