Change language

Python | __import __ () function

But what if the name of the required module is known to us only at runtime? How can we import this module? You can use the built-in __import__() function __import__() . It also helps to import modules at runtime.

Syntax: __import __ (name, globals, locals, fromlist, level)

Parameters:
name : Name of the module to be imported
globals and locals : Interpret names
formlist : Objects or submodules to be imported (as a list)
level : Specifies whether to use absolute or relative imports. Default is -1 (absolute and relative).

Example # 1 :

# import numpy module
# this is equivalent to & quot; import numpy & quot;

np = __ import__ ( ’ numpy’ , globals (), locals (), [], 0 )

 
# numpy array

a = np.array ([ 1 , 2 , 3 ])

 
# prints the type

print ( type (a))

Exit :

 "class ’numpy.ndarray’" 

Example # 2 :
Both of the following statements have the same meaning and do the same job.

# from numpy import complex as comp, array as arr

np = __ import__ ( ’numpy’ , globals < / code> (), locals (), [ ’complex’ , ’ array’ ], 0 )

 

comp = np. complex

arr = np.array

Application :
__import__() is not really required in day to day Python programming. Its direct use is rare. But sometimes, when you need to import modules at runtime, this feature is quite useful.

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically