Packages

Packages

A package is a pre-built set of functions and data types that can be loaded into a Python session to extend the language’s functionality.

We can import a package using the command import package_name.

Namespaces

When we import a package, we create a new namespace for the contents of that package. A namespace is effectively a naming system that helps us to avoid naming conflicts. We don’t always know the names of every object in a package that we are importing, and there might be situations where an object in a package has a name that conflicts with an object we have previously defined. By default, the name of a namespace for a package is the same as the name of the package, and we access its contents using the following syntax:

package_name.object_name

The math Package

Base Python includes many mathematical functions and operations, but there are several common mathematical functions that are missing. Several of these are included in the math package.

import math

The math packages contains functions the following functions (along with many others):

  • sqrt() - Returns the square root of the argument.

  • factorial() - Returns the factorial of the argument.

  • exp() - Returns the value of e raised to the argument.

  • log() - Returns the natural logarithm of the argument. An optional base parameter can be specified to change the base.

print('Square root of 20: ', math.sqrt(20))
print('10 factorial:      ', math.factorial(10))
print('e raised to 5:     ', math.exp(5))
print('Natural log of 100:', math.log(100))
print('Common log of 100: ', math.log(100, 10))
Square root of 20:  4.47213595499958
10 factorial:       3628800
e raised to 5:      148.4131591025766
Natural log of 100: 4.605170185988092
Common log of 100:  2.0

The math package also contains variables. For example, it contains an object named pi which stores the value of the constant pi. This package also includes trig functions such as sin, cos, and tan.

print(math.pi)
print(math.sin(math.pi/3))
print(math.cos(math.pi/3))
print(math.tan(math.pi/3))
3.141592653589793
0.8660254037844386
0.5000000000000001
1.7320508075688767

Using Aliases

If we are making frequent use of a certain package, it can b tedius to type its name every time you want to use an object from the package, especially if the name of the package is long. Python allows us to set an alias for a package when we import it. Setting an alias allows us to choose the name of the namespace associate with the package. The syntax for this is:

import package_name as alias
import math as mt
print(mt.pi)
print(mt.sin(mt.pi/3))
print(mt.cos(mt.pi/3))
print(mt.tan(mt.pi/3))
3.141592653589793
0.8660254037844386
0.5000000000000001
1.7320508075688767

Importing into the Main Namespace

If we have an object that we know we will be using frequently, we can import it directly into the main namespace as follows:

from package_name import object_name

Note that this only imports the specified object. Nothing else from the package is imported.

from math import pi

print(pi)
print(sin(pi))
print(cos(pi))
3.141592653589793
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-250b477389d1> in <module>
      2 
      3 print(pi)
----> 4 print(sin(pi))
      5 print(cos(pi))

NameError: name 'sin' is not defined

We can import several objects with a single import statement.

from math import pi, sin

print(pi)
print(sin(pi))
print(cos(pi))
3.141592653589793
1.2246467991473532e-16
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-40c1ec5e37ad> in <module>
      3 print(pi)
      4 print(sin(pi))
----> 5 print(cos(pi))

NameError: name 'cos' is not defined

If we want to import all objects from a package into the primary namespace, we can do so with the following command:

from package_name import *

Be careful with this command. If there are any naming conflicts, objects that had been created previously might be over-written.

from math import *

print(pi)
print(sin(pi))
print(cos(pi))
3.141592653589793
1.2246467991473532e-16
-1.0