1

I've got a Tkinter Python program, a reduced version of which can be found below:

from tkinter import *
from tkinter.ttk import *

filedialog.askopenfilename()

When I run this script from IDLE, I do not get any errors.

However, when run from PowerShell, using python myscript.py I get

NameError: could not find name 'filedialog'

Windows 10 x64 on a mid-2012 MacBook Pro

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
JPeroutek
  • 558
  • 1
  • 7
  • 20
  • My answer here: https://stackoverflow.com/questions/38806673/importing-from-a-package-in-idle-vs-shell/38808437#38808437 described how to fix existing IDLE releases so they give the proper error. – Terry Jan Reedy Aug 06 '16 at 20:48
  • @TerryJanReedy Thanks for fixing this! Much appreciated! – JPeroutek Aug 06 '16 at 22:25

1 Answers1

1

IDLE is probably importing it already, but in general since filedialog is a tkinter module it won't get imported with the bare:

from tkinter import *

Include an extra:

from tkinter import filedialog

and you should be good to go.

Feneric
  • 853
  • 1
  • 11
  • 15
  • Why would the filedialog module not be included in the *? – JPeroutek Jul 09 '16 at 00:55
  • 1
    Same as `ttk` (that you're already including separately), it's a module. – Feneric Jul 09 '16 at 01:42
  • 1
    This is a known IDLE bug in idlelib.run. https://bugs.python.org/issue25507 The solution in all cases is to add the missing import, as Feneric said. I hope to do the needed refactoring before 3.6, but must add tests first to avoid introducing new, and perhaps worse, bugs. – Terry Jan Reedy Jul 09 '16 at 20:51
  • The Q&A persuaded me to finish the issue. Starting with 3.5.3 and 3.6.0a4, "from tkinter import *; filedialog' will raise the appropriate NameError when run by IDLE. – Terry Jan Reedy Jul 17 '16 at 01:05