×

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

Find key from value in dictionary python

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

5 minutes read.

Python GUI Programming

GUI (Graphical User Interface) GUI is a graphics-based operating system that uses icons and menus to interact with the user. Python mostly works on CLI (Command Line Interface). Widgets Any user interface has...

4 minutes read.

Python Variables

Variables are one of the most important terms we should be familiar with if we want to be good programmers. In simpler words, we use variables to store a value....

4 minutes read.

Python Comments

In this tutorial, we will discuss the importance of comments in the Python code and how various types of comments can be inserted in the code. Comments are used to describe...

3 minutes read.

GET and POST requests using Python

‘GET’ and ‘POST’ are two request methods of Hypertext Transfer Protocol (HTTP). What is HTTP? HTTP stands for Hypertext Transfer Protocol. It is a collection of protocols which makes communication between client...

4 minutes read.

Python Ways to find nth occurrence of substring in a string

Introduction In Python, a string is a collection of characters that can be employed to conduct additional operations. In Python, a substring is a group of characters that are a part...

4 minutes read.

Unit Testing in Python

The process of testing whether a particular unit is working properly or not is called “UNIT TESTING”. A unit test will check small components in your application. The first and...

7 minutes read.

How to check data type in python

The data type represents the nature of a variable; it determines what kind of data it can store and what operations can be carried out on it. There are five...

4 minutes read.

Python String decode() method

Python String decode() method The string.decode() method decodes the string using the codec registered for encoding. Default encoding is the current default string encoding. It may raise errors to set a different error handling...

1 minute read.

Python pycountry

What is Pycountry? Python programming language: 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...

4 minutes read.

Python program to find the area of a circle

Python program to find the area of a circle This article will discuss how to find the area of a circle in Python with a given radius. The area of a...

2 minutes read.

How to Download all Modules in 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....

3 minutes read.

Uint8 Python

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.

Cross Entropy in Python

Introduction Cross-entropy loss is frequently combined with the softmax function. Determine the total entropy among the distributions or the cross-entropy, which is the difference between two probability distributions. For the purpose...

5 minutes read.

Python Set symmetric_difference() method

Python Set symmetric_difference() method The set.symmetric_difference() method returns a new set, which is the symmetric difference of two sets. The returned set contains only the unique items and, hence, deleting the common elements of...

2 minutes read.

Check if a String is Empty in Python

The string is one of the data types of Python. This data type stores all kinds of data but all the characters must be enclosed using double quotes (""). In...

5 minutes read.

Python abs() function

Python abs() function The abs() function returns the absolute value of a number. Syntax abs(x) Parameter x: The parameter ‘x’ can be an integer value, a floating point number or a complex number. Return This function returns...

1 minute read.

How to Take Input in Python

How To Take Input In Python Python has various in-built functions that help in code redundancy. Let's discuss the usage of some functions- ascii() – it returns a readable representation of objects.format()...

4 minutes read.

Python if statement

Python if statement Decision making is an essential feature of any programming languages. Condition checking is the strength of decision making. Python provides many decision-making statements, such as: If statementIf-else statementelif statement if statement The if statement is...

2 minutes read.

Sklearn in Python

Scikit-learn or sklearn is a machine learning library used in Python that provides many unsupervised and supervised learning tools and algorithms. David Cournapeau first created it as a 2007 Google...

7 minutes read.