Python Forum

Full Version: Importing modules
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
What is the difference between:
import my_module
and
from my_module import *
Aren't they the same?
They both import the module. However, one of them will require you to prefix any references to the module's contents, while the other should be avoided at all cost.
No they are not the same,the first one do not pollute the global namespace.
* do pollutes the global namespace.
>>> from pprint import pprint
>>> import math

>>> math.sin(4)
-0.7568024953079282

>>> pprint(globals())
{'__annotations__': {},
 '__builtins__': <module 'builtins' (built-in)>,
 '__doc__': None,
 '__loader__': <class '_frozen_importlib.BuiltinImporter'>,
 '__name__': '__main__',
 '__package__': None,
 '__spec__': None,
 'math': <module 'math' (built-in)>,
 'pprint': <function pprint at 0x0589F2B8>}
Now * import,all names from math module get imported to global namespace.
As mention should be avoided at all cost.

If need shorter name call use eg from math import sin()
Then it called the same way sin(4),then it easier to understand the import.
With * that import all names,it can be hard to understand that a name comes from a specific module.

>>> from pprint import pprint
>>> from math import *

>>> sin(4)
-0.7568024953079282

>>> pprint(globals())
{'__annotations__': {},
 '__builtins__': <module 'builtins' (built-in)>,
 '__doc__': None,
 '__loader__': <class '_frozen_importlib.BuiltinImporter'>,
 '__name__': '__main__',
 '__package__': None,
 '__spec__': None,
 'acos': <built-in function acos>,
 'acosh': <built-in function acosh>,
 'asin': <built-in function asin>,
 'asinh': <built-in function asinh>,
 'atan': <built-in function atan>,
 'atan2': <built-in function atan2>,
 'atanh': <built-in function atanh>,
 'ceil': <built-in function ceil>,
 'copysign': <built-in function copysign>,
 'cos': <built-in function cos>,
 'cosh': <built-in function cosh>,
 'degrees': <built-in function degrees>,
 'e': 2.718281828459045,
 'erf': <built-in function erf>,
 'erfc': <built-in function erfc>,
 'exp': <built-in function exp>,
 'expm1': <built-in function expm1>,
 'fabs': <built-in function fabs>,
 'factorial': <built-in function factorial>,
 'floor': <built-in function floor>,
 'fmod': <built-in function fmod>,
 'frexp': <built-in function frexp>,
 'fsum': <built-in function fsum>,
 'gamma': <built-in function gamma>,
 'gcd': <built-in function gcd>,
 'hypot': <built-in function hypot>,
 'inf': inf,
 'isclose': <built-in function isclose>,
 'isfinite': <built-in function isfinite>,
 'isinf': <built-in function isinf>,
 'isnan': <built-in function isnan>,
 'ldexp': <built-in function ldexp>,
 'lgamma': <built-in function lgamma>,
 'log': <built-in function log>,
 'log10': <built-in function log10>,
 'log1p': <built-in function log1p>,
 'log2': <built-in function log2>,
 'modf': <built-in function modf>,
 'nan': nan,
 'pi': 3.141592653589793,
 'pow': <built-in function pow>,
 'pprint': <function pprint at 0x0618F2B8>,
 'radians': <built-in function radians>,
 'sin': <built-in function sin>,
 'sinh': <built-in function sinh>,
 'sqrt': <built-in function sqrt>,
 'tan': <built-in function tan>,
 'tanh': <built-in function tanh>,
 'tau': 6.283185307179586,
 'trunc': <built-in function trunc>}