brianj
brianj

Reputation: 69

Confused on import syntax

I was hoping someone could answer this. I am going through Mark Lutz's "Programming in Python".

If I issue the statement in an interactive session:

import tkinter 

this statement works:

tkinter.filedialog.askopenfilename

However the same statements fail in a script with the error, "module has no attribute filedialog". I can get it to work if I type in the script:

from tkinter.filedialog import askopenfilename

and then to use it just use "askopenfilename".

I am just trying to understand why it isn't working in the script. It was my understanding I could reference it through dotted notation, but either that isn't true or I am doing something wrong.

Upvotes: 2

Views: 76

Answers (1)

Kamejoin
Kamejoin

Reputation: 347

The problem here is that tkinter is a module and tkinter.filedialog is another module. So, since your code said "import tkinter" python only imports tkinter and not tkinter.filedialog.

Upvotes: 1

Related Questions