Reading Time: 1 minutes
Importing Modules in Python
A Module(or package as they are referred as in other languages) is a collection of code accessible using an import statement. Simply put, a module is a file containing Python code. It may have variables, functions, classes, print statements etc. Modules allow you to logically organize your code, putting related stuff in a single module. This makes the code easier to comprehend and easier to work with. You can import a module in 4 different ways:
- import some_module
- import some_module as alias
- from some_module import *
- from some_module import some_func
############################## 1. import some_module ########################## |
# Say, you have come up with an elementary yet handy function that tells whether the number supplied to it is odd or even. It should look something like this: |
def oddOrEven(number): |
if number % 2 = = 0 : |
print (number, "is even." ) |
else : |
print (number, "is odd." ) |
|
# Now, you wish to use the above function in some other program without having to type it again. So you decide to put it inside a .py file, say myModule.py. Place it anywhere on your computer. In the same folder, create another file, say, main.py, and open it with IDLE. |
import myModule |
|
myModule.oddOrEven( 31 ) |
|
myModule.oddOrEven( 18 ) |
|
### OUTPUT ### |
31 is odd. |
18 is even. |
# This is the basic import statement in action i.e. import some_module. The basic import statement is executed in two steps: |
# -> find the said module, load the code in it |
# -> define a name or names in the local namespace for the scope where the “import” statement occurs. |
# The 'local namespace' contains all the identifiers of the program. So the import statement brings the identifiers of the module in the namespace of the program in which the import statement is written. |
# In order to import multiple modules in a single statement: |
>>> import module1, module2, module3 |
# The above will execute the aforementioned dual steps for each module separately, just as if they were written in different import statements one after the other. |
############################## 2. import some_module as alias ############################## |
# Python allows us to refer to the imported module by giving it a convenient name. The name thus given is known as the alias of the module in the current program. |
import myModule as specialFunctions |
|
specialFunctions.oddOrEven( 31 ) |
specialFunctions.oddOrEven( 18 ) |
|
|
### OUTPUT ### |
31 is odd. |
18 is even. |
############################## 3. from some_module import * ############################## |
# This type of import statement is used when we do not want to qualify the imported functions i.e. if we are trying to call oddOrEven() imported from module myModule, we will call it by oddOrEven(num) and not by myModule.oddOrEven(num). |
from myModule import * |
|
oddOrEven( 31 ) |
oddOrEven( 18 ) |
|
|
### OUTPUT ### |
31 is odd. |
18 is even. |
############################## 4. from some_module import some_func ############################## |
# To import a specific function from a module, from some_module import * is used. This variant is handy when only few selected portions of code are to be imported from a large module. |
from myModule import oddOrEven |
|
oddOrEven( 31 ) |
oddOrEven( 18 ) |
|
|
### OUTPUT ### |
31 is odd. |
18 is even. |