0

Why for some modules do we type a '*' for example:

from tkinter import *

and for some of them we don't, like this one:

import time
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Arian
  • 57
  • 9

1 Answers1

3

When importing a package, objects from the package are in a namespace, e.g.,

import time
time.sleep(1)

But when explicitly importing objects from a package, they are imported into the global namespace, e.g.,

from time import sleep
sleep(1)

When using * instead of a list of object names, all objects from the package are imported into the global namespace and can be used without specifying the package name.

Corylus
  • 736
  • 5
  • 16
  • 2
    In [How to Answer](https://stackoverflow.com/help/how-to-answer), note the section _Answer Well-Asked Questions_, and the bullet point therein regarding questions that "have been asked and answered many times before". – Charles Duffy Jul 03 '22 at 13:58
  • 1
    I like this answer because it is concise, very easy to understand, and doesn't have all the "why this is bad and you shouldn't do it" arguments attached -- maybe it could be added to the first question linked at the top [what-is-the-reason-for-using-a-wildcard-import](https://stackoverflow.com/questions/55722260/what-is-the-reason-for-using-a-wildcard-import). – topsail Jul 03 '22 at 14:22