Python Language Importing modules Importing all names from a module


from module_name import *

for example:

from math import *
sqrt(2)    # instead of math.sqrt(2)
ceil(2.7)  # instead of math.ceil(2.7)

This will import all names defined in the math module into the global namespace, other than names that begin with an underscore (which indicates that the writer feels that it is for internal use only).

Warning: If a function with the same name was already defined or imported, it will be overwritten. Almost always importing only specific names from math import sqrt, ceil is the recommended way:

def sqrt(num):
    print("I don't know what's the square root of {}.".format(num))

# Output: I don't know what's the square root of 4.

from math import * 
# Output: 2.0

Starred imports are only allowed at the module level. Attempts to perform them in class or function definitions result in a SyntaxError.

def f():
    from math import *


class A:
    from math import *

both fail with:

SyntaxError: import * only allowed at module level