image
image
image

Import in Python

image

The programs we have run so far are small, but in reality, a program can be hundreds to ten thousand lines of code. In this case, a large program is broken into smaller units called modules. These modules are related but on different files normally ending with the extension .py. Python allows importing of a module to another module using the keyword import. Analogy: You probably have some of your certificates scanned and stored in your Google drive, have your notebook in your desk, have a passport photo in your phone external storage, and a laptop in your room. When writing an application for an internship, you will have to find a way of accessing all these resources, but in normal circumstances, you will only work with a few even though all of them are connected. The same is true for programs.

Example

Assume we need to access the math pi that is a different module. The following program will illustrate:

Start IDLE.

Navigate to the File menu and click New Window.

Type the following:

import.math  #referencing to the contents of math module

print(math.pi)  #Now utilizing the features found in that referenced math