wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, volunteer authors worked to edit and improve it over time.
This article has been viewed 24,224 times.
Learn more...
Python's syntax allows for code to be significantly shortened by using something called modules. Similar to header files in C++, modules are a storage place for the definitions of functions. They are separated into common uses, such as the time module, which provides functions for time related uses.
Steps
Using the from-import instruction
The from-import instruction imports functions from a module and lets you use them like functions from the core Python. You don't see that the functions belong to the module.
-
1
-
2To import a specific function from a specific module, write:
from [module] import [function]
- For example, to import the
randint
function from therandom
module and print a random number using that function, you would write:from random import randint print(randint(0, 5))
Advertisement - For example, to import the
-
3Separate multiple functions from the same module with commas (,). The structure looks like this:
from [module] import [function], [otherFunction], [anotherFunction], ...
- For example, to import the
randint
andrandom
functions from therandom
module and print random numbers using these functions, you would write:from random import randint, random print(randint(0, 5)) print(random())
- For example, to import the
-
4Import entire modules using a
*
instead of a function name. The structure looks like this:from [module] import *
- For example, to import the entire
random
module and then print a random number with itsrandint
function, you would write:from random import * print(randint(0, 5))
- For example, to import the entire
-
5Import multiple modules by writing multiple from-import instructions. You should start a new line for each instruction to keep the code readable, although separating them with a
;
also works.- For example, to import the
randint
function from therandom
module and thesqrt
function from themath
module and then print a result from both functions, you would write:from random import randint from math import sqrt # Would also work, but hard to read: # from random import randint; from math import sqrt print(randint(0, 5)) print(sqrt(25))
- For example, to import the
Using the import instruction
The import instruction imports functions from a module and leaves it visible that the functions are from that module. When using a function imported with the import instruction, you have to write the module name and a dot (.) before it.
The import instruction doesn't allow to import a single function from a module without also importing all others.
-
1
-
2To import a module, write with the following structure:
import [module]
- For example, to import the
random
module and then print a random number with itsrandint
function:import random print(random.randint(0, 5))
- For example, to import the
-
3Separate multiple modules with a comma (,). The structure is:
import [module], [otherModule], [anotherModule], ...
- For example, to import the
random
andmath
modules and then print the results of therandint
andsqrt
functions that are included in these modules, you would write:import random, math print(random.randint(0, 5)) print(math.sqrt(25))
- For example, to import the