Python Forum
Different Ways to Import Modules - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Different Ways to Import Modules (/thread-41273.html)



Different Ways to Import Modules - RockBlok - Dec-11-2023

Good morning,

I'm not entirely sure I understand the reason to use the asterix when importing modules, or what its significance might be, or why you would use it.

For example, I think doing something like this would import the entire module

import math

x = math.pi

print(x)
and I think doing something like this would import the specific function from the module?

from math import pi as why

x = why

print(x)
My question is, I don't understand the asterix when its used like this

from math import *

x = pi

print(x)



RE: Different Ways to Import Modules - menator01 - Dec-11-2023

It's a wildcard import. In the example, it will import all the math functions and classes. It is also bad practice to do wildcard imports.


RE: Different Ways to Import Modules - deanhystad - Dec-11-2023

And you should never use wildcard imports. Wildcard imports make it more difficult to know what names are imported from which modules, silently import names that might collide with variables/functions in your program or other modules imported using a wildcard