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:This will tell the script you are using a specific function from a specific module.
from [module] import [function]
- For example, to import the
randintfunction from therandommodule 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
randintandrandomfunctions from therandommodule 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
randommodule and then print a random number with itsrandintfunction, 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
randintfunction from therandommodule and thesqrtfunction from themathmodule 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
randommodule and then print a random number with itsrandintfunction:import random print(random.randint(0, 5))
- For example, to import the
-
3Separate multiple modules with a comma (,). The structure is:You can also make multiple import instructions on multiple lines if that appears more legible or makes more sense in your specific case.
import [module], [otherModule], [anotherModule], ...
- For example, to import the
randomandmathmodules and then print the results of therandintandsqrtfunctions 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


























































