0

I've browsed the similarly phrased questions, but haven't found an answer:

When importing SOME libraries, it seems like I have to import the top level library in one statement, and then import the module or object I want to use in a second statement. An example that I have recently come across is:

import tkinter as tk
import tkinter.filedialog

Which results in me being able to get a file dialog in two different ways:

files = filedialog.askopenfilenames()

or

files = tk.filedialog.askopenfilenames()

Why can I not call the second option without importing tkinter.filedialog? I'm not familiar with the specific structure of files/packages/modules/ etc. I assume it has something to do with it.

rocksNwaves
  • 5,331
  • 4
  • 38
  • 77
  • Who says you can't use the second option? What error did you get when you tried it? – Prune Nov 26 '19 at 18:27
  • @Prune I'm getting a "cannot find reference 'filedialog' in __init__.py" – rocksNwaves Nov 26 '19 at 18:30
  • @Prune, I see now that this was just a warning. The code compiled and ran despite the warning. – rocksNwaves Nov 26 '19 at 18:33
  • The second import line is unnecessary given the first line. You can just use `tk.filedialog.askopenfilenames()` without the need for the second line. – Run_Script Nov 26 '19 at 18:34
  • For me in Pycharm, without the second import, I get a stopping error: `AttributeError: module 'tkinter' has no attribute 'filedialog'`. With the second import, it runs just smooth. – JohanC Nov 26 '19 at 18:36

1 Answers1

2

EDITED: You can check out these answers for your question:

Python 3.6 - AttributeError: module 'tkinter' has no attribute 'filedialog'

Why tkinter module raises attribute error when run via command line but not when run via IDLE?

Nikhil Gupta
  • 1,436
  • 12
  • 15
  • The question seems to be: why does `files = tk.filedialog.askopenfilenames()` give an error if you only `import tkinter as tk` – JohanC Nov 26 '19 at 18:34
  • @JohanC it looks like the error was just a warning, though I am still curious as to why I would get any back talk from my IDE... so yeah I guess the question still stands. – rocksNwaves Nov 26 '19 at 18:36