Is there a reason why the code will raise an error when run via the command line compared to when run via IDLE\'s run module f5 command?
Actually it's true that the module has no attribute filedialog, it's a submodule and you should import it as import tkinter.filedialog before using it. You can use tk.filedialog without explicitly importing filedialog in IDLE because it's already imported.
import sys
sys.modules['tkinter.filedialog']
The above code will raise a KeyError in a standard python interpreter but it will return something like in IDLE.