×

Import Function in Python

In Python programming language, the import keyword plays an important role in the whole code because it makes one module make available in another module. Import is used to structure the programming code very efficiently. Import function allows us to reusable of code and making the past done work and projects maintainable.

Basically, Python basic import is well categorised into modules and packages and the module is defined as they are viewed into Python with the help of importing process and it is also defined as an object that comes up with an organizational unit of Python programming code.

Python only responds to the file containing the extension .py.

The power of modules is that they can be imported anytime and can be reused in other code as well.

Example:

>>>import math
>>>math.pi

Output:

3.141592653

In this piece of code, we observed that we import the math module and use it and the second line we can access the pi variable which is within the math module. math is present and is part of the Python’s standard library which means it is universal in the Python and can be accessed and made available while working with the Python.

NOTE:

While importing we write math.pi rather than just pi because it is not only a module but also a namespace that keeps all the attributes of module together. It makes the code readable and organized in well manner.

We can import any module into the current program to use the functionality. If we want to import the module, we need to use the import keyword with the respective module name associated with. While interpreting the code, if import statement appears then it imports directly into our program. We can also use the functions inside a module by using the .dot () operator along with the associated module name.

For working of the code more efficiently, it is better to import one module only once per the interpreter window. If there is a chance in changing the modules we must again start the interpreter. And if we want test only one module iteratively just uses reload () with the module name with it.

reload(module name)

There are 3 types of ways importing modules:

  • From .. import statement
  • From ..import * statement
  • Renaming the imported module

From .. import statement:

This type is used to import only specific categorized functions and variables from a whole module instead of importing all the functions which are not necessary when choosing a particular function. It is used to import a specific kind of function from the main Python module.

Example:

>>>form math import add
>>>Print (add(1, 2))

Output:

3

We can make the use of add () directly without implementing the module name. when it comes to the point of implementation of multiple attributes at a once, we need to separate the multiple attributes with just a comma and the syntax is followed as:

From calculation import add,sub

Here, add and sub are two multiple attributes separated by comma in between.

From ..import * statement:

This type of statement is a drawback in the Python language because if we implement this statement we disturb the concept of namespace and the function import* is when used, imports all the related functions and classes into the program. These may create a conflict among the other defined functions and other libraries that we imported.

The main disadvantage of this format is that code readability and hiding of the bugs within the program.

Example:

from calculation import *
print(add(2, 3))
print(sub(3, 2))

Output:

5
1

Renaming the Imported Module:

We can rename the module that we are importing, and it comes to the use when the module name is large or wants to a give a short name to understand it easily. We need to use the keyword as and rename it.

Example:

import calculation as cal
print (cal.add(1, 2))

Output:

3

Once the module is renamed, we can’t access the first named module because it’s not going to be recognised anywhere in the program.

Here, the calculation is renamed as cal and calculation is not accessible anywhere in the code.

After all importing the modules, if we want to list out all the names used in the particular module we need to use the dir() function which gives the list.


Related Topics

How to plot multiple linear regression in Python

This article lets us look into linear regression in Python and how we can plot multiple linear regressions in it. Linear regression One of the simplest and most widely used Machine Learning...

4 minutes read.

Sentiment Analysis using NLTK

Introduction Data is being produced at an astounding rate and volume in the field of the internet and other digital services nowadays. Researchers, engineers, and data analysts often work with tabular...

7 minutes read.

Not supported between instances of str and int in python

In this article, you are going to learn why the type error occurs between instances of string and integer datatypes. Also, you will know what kind of error is the...

6 minutes read.

Python Logging Maxbytes

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

6 minutes read.

Python Marshmallow

Python:  Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Difference between Python 2 and Python 3

In this tutorial, we will learn the differences between two versions of python, that is, python version 2 and python version 3. Some basic differences include- Python 2 is the older version...

3 minutes read.

Data Structures and Algorithms Using Python | Part 1

Data Structures: Data Structure is defined as a way to organize and store the data so that we can access the data and work more efficiently. Data structures also describe the...

18 minutes read.

How to Configure Python Interpreter in Eclipse

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python Program to Find the gcd of Two Numbers

Introduction Greatest Common Divisor is the full form of gcd. The greatest common divisor, or GCD, of two numbers, is a value that can exactly divide the two digits and is...

5 minutes read.

Importing Numpy in Pycharm

Numpy : Numpy is one of the important library in python. The abbreviation of numpy is “Numerical python” or “Numeric Python”. This library is mainly used to access arrays. Numpy was created...

5 minutes read.

Working with CSV files in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

7 minutes read.

Pillow Python introduction and setup

Introduction Advanced image processing implies handling the picture carefully with the assistance of a PC. Utilizing picture handling we can perform activities like upgrading the picture, obscuring the picture, separating text...

4 minutes read.

Get Bounding Box Co-ordinates Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

6 minutes read.

Python MySQL Update Operation

Python MySQL Update Operation: In this part of tutorial, we will learn that how can we update a table present in SQL database through our Python program. As like SQL,...

4 minutes read.

Python NewLine

In python, a new line character denoted by "\n" is known as an escape character or escape sequence, and it will force the cursor to change its position to the next...

6 minutes read.

What is Collaborative Filtering in ML, Python

Introduction Contents recommendation is a useful tactic for almost any specific technology looking to increase interest, but it frequently calls for a lot of user data and perhaps laborious content tagging...

3 minutes read.

Write Dictionary to CSV in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

4 minutes read.

Python List extend() method

Python List extend() method The list.extend() method extends the list by appending all the items from the iterable. Syntax list.extend(iterable) Parameter iterable: It is a required parameter which represents any iterable unlike list, set, tuple, etc. Example 1 # Python...

1 minute read.

Salary of Python Developers in India

In this tutorial, we will understand who python developers are and what is their salary when they work in India. Python Developers Python Developers are the people who are involved in designing...

4 minutes read.

Python MySQL

In this article, we are going to learn the following: How to connect Python to MySQL.How to create a new Database.Procedure for connecting the newly created database.Procedure for connecting the already...

7 minutes read.