__import__() in Python - Scaler Topics (2024)

Overview

The __import()__ in the python module helps in getting the code present in another module by either importing the function or code or file using the __import__() in the python method. Whenever we need a specific module while executing the code, we can use this __import__() in the python module and import the libraries we want. Here we use the __import__() function to call the import statement.

Syntax of __import__ function in Python

The syntax of __import__() in the python method is given by:

where name is the module that we want to import for our use.

Parameters of __import__ function in Python

The __import__() in the python method has the following parameters, which are given at the import method's time.

  • name - This is the name of the library where the code present that we want to import or use is specified.
  • globals - By determining global, we specify how the name should be interpreted.
  • locals - By determining locals, we specify how the name must be interpreted.
  • fromlist - These are the submodules or the objects that must be specified to import them by name, mostly as a list.
  • level - This determines if the module that we are about to import should be absolute or relative imports. The default import is -1(absolute and relative).

Return Values of __import__ function in Python

The import in python returns the object or module that we specified while using the import module.

Exceptions of __import__ function in Python

We cannot say this as an exception, but we need to know that the use of the __import__() function in python is rarely used and not an everyday practice while executing code.

This is so because when we use the __import__() function, we can alter the semantics of the specified import statement, which is not a good practice and is highly discouraged too.

As we understood above, to call the import statement, we make use of the __import__() function, and this function holds the capability to alter the semantics of the import statement, which happens as this statement calls this function.

To avoid this, it is recommended to import hooks instead. Also we can also make use of importlib.import_module() for importing the module by name.

Example of __ import __() function in Python

Let us understand by the below example.

Code:

Output:

Explanation:

In the above example, we see how we can use the __import__() function in python. We also saw how the use of the __import__() function is discouraged as we are able to access the fabs() function by creating a new module which can lead to confusion sometimes. As shown above, we are able to access fabs in two ways: one is its original math module and the other as we imported it as maths_1.

This is discouraged as a change of semantics can lead to confusion. The fabs() function in python comes under the math module where we can get the absolute value of the number as a float.

What is the __ import __() Function in Python?

We can describe the import in python function as a module that contains the pre-defined codes that can be called via the import statement, which decreases the redundancy of code reuse. We mostly use the __import__() in python module for getting the code present that is present in another module by either importing the code or calling the function or file using the import__() in the python method.

We studied the syntax and its parameters, where describing the level is very important as it specifies which import type to choose. We have two import levels - Absolute and Relative.

  • Absolute Import - An absolute import determines that the import of the resources must be using the full path from the project’s root folder.
  • Relative Import - The relative import determines the import of the resources to happen from the current location, which is the location of the execution of the import statement happened.

We make use of the import in python in the rare case, but when we are executing the code and require a certain module during runtime, this import in the python function comes really handy.

How to use __ import __() in python?

We can use import by simply using its syntax (descriptive use can be seen in the following examples) as below:

We need to keep in mind to specify the parameters accordingly. In rare cases we use the __import__() in python and regular use of its is highly discouraged as well. This is so because it holds the capability to change the semantics of the import statement which happens as this statement calls this function.

More Examples

Example 1: To import NumPy Module

Lets us make the use of__import__() in python to import the numpy module as below.

Code:

Output:

Explanation:

As seen from the above code, we saw how we can import the module from another module and execute the code. We simply imported numpy as an array by specifying the relevant parameters. This is similar to using import numpy as np.

Conclusion.

  • The __import__() in python module helps in getting the code present in another module by either importing the function or code or file using the import in python method.
  • The import in python returns the object or module that we specified while using the import module.
  • The syntax of __import__() in python method is given by:
  • where,
    • name - Name of the library we want to import.
    • globals and locals - interpret the name.
    • fromlist - The submodules or objects that are required to import as a list
    • level - Determines whether to import as absolute or relative imports, default being -1(absolute and relative)
  • The use of the __import__() function in python is not an everyday practice and is rarely used. We discourage the use of the __import__() function in python as it holds the capability to alter the semantics of the import statement which happens as this statement calls this function.
  • zfill() in Python
  • Swapping in Python
  • any() in Python
  • all() in Python
__import__() in Python - Scaler Topics (2024)
Top Articles
Insurance Definition & Meaning | Britannica Dictionary
What is a Monthly Average Balance | ICICI Bank
Knoxville Tennessee White Pages
Design215 Word Pattern Finder
Ingles Weekly Ad Lilburn Ga
PRISMA Technik 7-10 Baden-Württemberg
DL1678 (DAL1678) Delta Historial y rastreo de vuelos - FlightAware
Craigslist In South Carolina - Craigslist Near You
Tanger Outlets Sevierville Directory Map
A Guide to Common New England Home Styles
Hca Florida Middleburg Emergency Reviews
Drago Funeral Home & Cremation Services Obituaries
سریال رویای شیرین جوانی قسمت 338
Magic Mike's Last Dance Showtimes Near Marcus Cedar Creek Cinema
Grasons Estate Sales Tucson
Katherine Croan Ewald
Grayling Purnell Net Worth
Healthier Homes | Coronavirus Protocol | Stanley Steemer - Stanley Steemer | The Steem Team
Uta Kinesiology Advising
Kaitlyn Katsaros Forum
Johnnie Walker Double Black Costco
Jeff Nippard Push Pull Program Pdf
Greensboro sit-in (1960) | History, Summary, Impact, & Facts
Booknet.com Contract Marriage 2
When His Eyes Opened Chapter 3123
Delta Township Bsa
Ascensionpress Com Login
Marlene2995 Pagina Azul
Skepticalpickle Leak
897 W Valley Blvd
Craigslist Neworleans
No Hard Feelings Showtimes Near Tilton Square Theatre
Police Academy Butler Tech
Samsung 9C8
RALEY MEDICAL | Oklahoma Department of Rehabilitation Services
Legit Ticket Sites - Seatgeek vs Stubhub [Fees, Customer Service, Security]
2023 Fantasy Football Draft Guide: Rankings, cheat sheets and analysis
Silive Obituary
Cocorahs South Dakota
Academic Calendar / Academics / Home
Eat Like A King Who's On A Budget Copypasta
Rite Aid | Employee Benefits | Login / Register | Benefits Account Manager
Bank Of America Appointments Near Me
Dlnet Deltanet
Lebron James Name Soundalikes
Bedbathandbeyond Flemington Nj
What Does the Death Card Mean in Tarot?
How To Win The Race In Sneaky Sasquatch
All Obituaries | Roberts Funeral Home | Logan OH funeral home and cremation
Latest Posts
Article information

Author: Mr. See Jast

Last Updated:

Views: 6541

Rating: 4.4 / 5 (75 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Mr. See Jast

Birthday: 1999-07-30

Address: 8409 Megan Mountain, New Mathew, MT 44997-8193

Phone: +5023589614038

Job: Chief Executive

Hobby: Leather crafting, Flag Football, Candle making, Flying, Poi, Gunsmithing, Swimming

Introduction: My name is Mr. See Jast, I am a open, jolly, gorgeous, courageous, inexpensive, friendly, homely person who loves writing and wants to share my knowledge and understanding with you.