bike et et aeiboc ee kere ~
How to import modules in Python?
How to Import Modules in Python
In this lesson, you will learn how to import modules into your Python projects.
Method 1: import module_name
We write import module_name to import a module into our program.
import module_name
Let's import a module,
In [1]: import math #imports the math module. We can now use all the functions of this module.
We can use the dir() builtin function to learn what there is in a module.In [2]: dir(math) #Lists all of the functions that the module contain
ovt[2]: ['_doc_',
"Toader,
Tame
‘package _",
—spec_'
“acos',
‘acosh’,
‘asin’,
‘asinh’,
‘atan',
‘atan2',
‘atanh’,
‘ceil’,
‘comb’,
“copysign’,
cos",
‘cosh’,
‘degrees’,
‘dist’,
re’,
‘erf',
‘erfc',
‘exp’,
‘expmi',
‘fabs’,
‘factorial’,
‘Floor’,
“fnod' ,
“frexp',
‘sum’,
‘ganna’,
“ged”,
"hypot',
inf’,
sclose',
isfinite’,
‘isin#’,
‘isnan’,
‘isgrt
"Idexp',
“Iganma",
‘log’,
"1ogie",
‘logip',
“log2",
‘mod,
‘nan’,
‘perms
pi’,
‘pow’,
‘prod’,
‘radians’,
‘remainder’,
‘sin’,
‘sinh’,
‘sqrt’,
‘tan’,
‘tanh’,
‘tau’,
“trunc’]In [3]:
In [4]:
In [5]:
out[s]:
In [6]:
out[6]:
In [7]:
help(math) # Shows aLl of the information of the module using the help() function.
Keturn the arc cosine (measurea in ragians) ot x.
acosh(x, /)
Return the inverse hyperbolic cosine of x.
asin(x, /)
Return the arc sine (measured in radians) of x.
asinh(x, /)
Return the inverse hyperbolic sine of x.
atan(x, /)
Return the arc tangent (measured in radians) of x.
atana(y, x, /)
Return the arc tangent (measured in radians) of y/x.
Unlike atan(y/x), the signs of both x and y are considered.
atanh(x, /)
So, how can we use the functions that located in the math module?
module_name. function_name()
Let's look at what factorial function that located in math module does.
help(math. factorial)
Help on built-in function factorial in module math:
factorial(x, /)
Find x1.
Raise a ValueError if x is negative or non-integral.
math. factorial (5)
120
math factorial (8)
40320
help(math.trunc)
Help on built-in function trunc in module math:
trunc(x, /)
Truncates the Real x to the nearest Integral toward 0.
Uses the _trunc__ magic method.In [8]:
out[s]:
In [9]:
In [10]:
out(1@]:
In [11]:
In [13
out[13]:
In [14]:
In [15.
out[15]:
In [16]:
out(16]:
math.trune(2.3)
2
help(math. ceil)
Help on built-in function ceil in module math:
ceil(x, /)
Return the ceiling of x as an Integral.
This is the smallest integer >= x.
math. ceil(5.4)
help(math. ceil)
Help on built-in function ceil in module math:
ceil(x, /)
Return the ceiling of x as an Integral.
This is the smallest integer >= x.
help(math. floor)
Help on built-in function floor in module math:
floor(x, /)
Return the floor of x as an Integral.
This is the largest integer
math floor(5.4)
So, can we use our module with an alias? The answer is Yes.
import math as m
m.factorial(6) # We can use our function with "nm"
720
m. factorial (3)
Method 2: from module_name import *
The other way of importing module is writing
instead of writing it's original namefrom module_name import *
In [17]: from math import * #imports all the functions of math module using an asterisk.In [18]: dir(math)
out[i8]: ['_doc_',
"Toader,
Tame
‘package _",
—spec_',
"acos',
‘acosh’,
‘asin’,
‘asinh',
‘atan',
‘atan2',
‘atanh’,
‘ceil’,
“comb',
“copysign’,
cos",
‘cosh’,
‘degrees’,
‘dist’,
re’,
‘erf',
‘erfc',
‘exp’,
‘expm',
‘fabs’,
‘factorial’,
‘Floor’,
“fnod' ,
“Frexp',
‘fsum',
‘ganna’,
‘gcd’,
"hypot',
inf’,
sclose',
isfinite’,
‘isin#’,
‘isnan’,
‘isgrt
"Idexp',
“Iganma",
log",
"1ogie",
‘logip’,
“log2",
‘mod,
‘nan’,
‘perms
pi’,
‘pow’,
‘prod,
‘radians’,
‘remainder’,
‘sin’,
‘sinh’,
‘sqrt’,
‘tan’,
‘tanh’,
‘tau’,
“trunc’]
So, how can we use the functions of the modules with using method2?In [19]:
out[19]:
In [20]:
out [20]:
In [24]:
out[21]:
In [22]:
In [23]:
out[23]:
In [24]:
out [24]:
In [25]:
out[25]:
In [26]:
To use functions, we write just the function names, we don't need to write our module names in this
method.
Here is the syntax: «
function_name()
factorial(3)
6
factorial(5)
120
floor(3.4)
We can get particular functions instead of get
19 alll the functions in this module with using this method.
So, how can we do that?
To do that, we need to specify the function names that we want to import into our program explicitly.
But, before doing this, we need to restart the Jupyter Notebook from Kernel Menu, because, we have
imported this module above, with it's all the functions. And for this reason, we need to restart this Notebook
again to see this clearly,
from math import factorial,trunc # imports just these two specific functions.
factorial(5)
120
trunc(3.5)
3
ceil(3.4) # We couldn't use this function. Because we didn't import this function.
4
So, what is the difference between 2 two methods?
‘As you know, when you use our second method, we write just the function names and we can import
particular functions that we want to import.
And if we use the second function and if we have two functions with the same names, the latest function
that | wrote or imported overrides the previous one. Please, keep that in mind if you use this method while
importing a module. If you have a situation that you may confuse, you can prefer the first method,
Let's restart our Jupyter Notebook again.
from math import *In [27]: def factorial (number):
print("My Factorial function!")
fact = 1
if (number
return 1
while (number >= 1):
fact *= number:
number -= 1
return fact
@ or number
In [28]:
factorial(5)
My Factorial function!
out[28]:
120
That's the end of this lesson. You will learn how to write your own modules in Python in the next lessons.
See you in the next lesson.