#python #tkinter #import

I don't get the processes behind "import" and "from xyz import". When I code:

from tkinter import *

then I import, I thought, the whole tkinter names into my namespace. But I can't use neither

tkinter.messagebox.showinfo("title", "someinfo")
nor
messagebox.showinfo(...)

To use that, I have to

from tkinter import messagebox

to access it. the same with

import tkinter as tk

... there is no:

tk.messagebox.showinfo...

@fatrat `from … import *` doesn’t import everything. The module’s author can decide what’s included.

But note that it’s generally considered bad practice to use `from … import *` as it dumps lots of stuff into the main namespace. It is safer to keep namespaces separate

@s_gruppetta @fatrat

"import * doesn’t import everything. The module’s author can decide what’s included."

Thank you! I've stumbled into this several times and did not realize what was happening.

@JHB17 @fatrat Although in general, it's always best to avoid `from ... import *`

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.