Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing modules
#1
What is the difference between:
import my_module
and
from my_module import *
Aren't they the same?
Reply
#2
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.
Reply
#3
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>}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  confusion on importing modules carter187 4 342 Mar-03-2024, 08:11 PM
Last Post: snippsat
  Importing modules from different folders Tomli 3 1,428 Jun-26-2022, 10:44 AM
Last Post: snippsat
  Importing modules issue mp3909 9 3,473 Jun-24-2020, 10:07 PM
Last Post: snippsat
  Importing Custom Modules in Python 3 Flexico 1 2,544 Aug-24-2019, 08:11 PM
Last Post: snippsat
  Trouble importing modules on a new python version snackman_barry 2 2,521 Jul-12-2019, 11:15 AM
Last Post: snackman_barry
  importing modules PiaNa 1 1,931 Jun-24-2019, 12:50 PM
Last Post: ichabod801
  Importing all modules and using it rohitnirantar 2 2,518 Aug-28-2018, 08:15 PM
Last Post: snippsat
  Importing modules Pistolpete 2 2,671 Nov-29-2017, 05:24 PM
Last Post: nilamo
  Modules issue, pip3 download modules only to pyhton3.5.2 not the latest 3.6.1 bmohanraj91 6 8,351 May-25-2017, 08:15 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020