2

I have just learnt about importing modules, and I am a bit confused about the wildcard import.

from module_name import *

I do not understand the reason of using it at all, I see people saying not to use it at all.

Could someone clarify what it really means, and why would one use it?

CristiFati
  • 38,250
  • 9
  • 50
  • 87
Jperkins98
  • 67
  • 1
  • 10
  • it means you import everything of the module and you have small modules where it can but at realy big modules you will get a lot of loading time and your program will become realy slow – Matthijs990 Apr 17 '19 at 07:37
  • If you're going to use every thing which was exported from a module, then importing each thing individually is a redundant, isn't it? Your other option is to import only the `module` and then use `module.thing` everywhere. Neither solution yields good looking code – rdas Apr 17 '19 at 07:40
  • Suppose you have a module util.py with 100 functions. Now that you want to use 50 of them, you might want to use `from util import *` instead of importing them one by one. – DDGG Apr 17 '19 at 07:50
  • 1
    One example why wildcard imports are bad: Do `import os` and wonder why [`open`](https://docs.python.org/3/library/os.html#os.open) suddenly doesn't work like [`open`](https://docs.python.org/3/library/functions.html#open) anymore. – Matthias Apr 17 '19 at 08:08
  • @Matthias i think you meant `from os import *` –  Apr 17 '19 at 08:31
  • 2
    @reportgunner: That was an `OutOfCoffeeError`. You're right. – Matthias Apr 17 '19 at 08:39
  • haha I was just running my `moar_coffee()` routine –  Apr 17 '19 at 08:43

3 Answers3

4

According to [Python.Docs]: Modules - More on Modules (emphasis is mine):

There is even a variant to import all names that a module defines:

>>> from fibo import *
>>> fib(500)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

This imports all names except those beginning with an underscore (_). In most cases Python programmers do not use this facility since it introduces an unknown set of names into the interpreter, possibly hiding some things you have already defined.

Note that in general the practice of importing * from a module or package is frowned upon, since it often causes poorly readable code. However, it is okay to use it to save typing in interactive sessions.

So, it means: importing all (check the above page for the __all__ variable meaning) symbols exported by a module / package into the current namespace.

Generally (as the above states), it's used when one is in the console and wants to save time by not importing everything needed, "manually".
It's also used by some who don't know what to import (so they import everything, since they don't really know what they're doing - there are exceptions of course, but those are rare).

Anyway, probably this is the most eloquent example (as it only relies on Python): illustrating its pitfalls:

>>> with open("out.txt", "w") as f:
...     f.write("DON'T USE wildcard imports!")
...
27
>>>
>>> from os import *
>>>
>>> with open("out.txt", "w") as f:
...     f.write("USING wildcard imports ...")
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required (got type str)

The wildcard import shadows:

by:

Things can get even messier when dealing with 3rd-party modules (where the collision hit chance can grow exponentially).

CristiFati
  • 38,250
  • 9
  • 50
  • 87
1

from module import * generally imports evey name from a given module (though the module may use __all__ to restrict that). It's generally best avoided, since this set of names may change over time, possibly changing the names available to your code.

I do sometimes use it in interactive sessions as a convenience, though.

brunns
  • 2,689
  • 1
  • 13
  • 24
0

This is used for importing everything from the module. The reason why you are recommended not to use it, is because it can get confusing as to where the function or class you are using came from. Moreover, some things might have the same name in different modules, and importing them like this would overwrite the one previously imported.

Kushagra Gupta
  • 514
  • 2
  • 13
  • Here is a reference for this statement: According to PEP8 "Wildcard imports (from import *) should be avoided" (see https://www.python.org/dev/peps/pep-0008/#imports). – BR123 Jun 10 '21 at 16:27