Import module in Python - GeeksforGeeks (2024)

Last Updated : 11 Dec, 2023

Summarize

Comments

Improve

Import in Python is similar to #include header_file in C/C++. Python modules can get access to code from another module by importing the file/function using import. The import statement is the most common way of invoking the import machinery, but it is not the only way.

Import Modules in Python

When we import a module with the help of the Python import module it searches for the module initially in the local scope by calling __import__() function. The value returned by the function is then reflected in the output of the initial code.

Python3

import math

pie = math.pi

print("The value of pi is : ", pie)

Output

The value of pi is : 3.141592653589793

Importing Module using “from”

In the above code module, math is imported, and its variables can be accessed by considering it to be a class and pi as its object.The value of pi is returned by __import__().pi as a whole can be imported into our initial code, rather than importing the whole module.We can also import a file in Python using from.

Output

3.141592653589793

Import Python Built-In Modules

In this example, the built-in ‘random’ module is imported in Python using the import statement. The randint function from the ‘random’ module is then utilized to generate a random integer between 1 and 10, and the result is printed. We can also import a file in Python by using import statement.

Python3

# Import the 'random' module, which is a built-in module for random number generation

import random

# Generate a random number between 1 and 10

random_number = random.randint(1, 10)

print("Random Number:", random_number)

Output

Random Number: 5

Import a Module and Assign an Alias in Python

In this example, the ‘math’ module is imported with the alias ‘m’ using the import statement. The sqrt function from the ‘math’ module, accessed through the alias ‘m’, is then used to calculate the square root of 25. The result is printed as “Square root of 25.

Python3

# Import the 'math' module with the alias 'm'

import math as m

# Use functions from the 'math' module with the alias

result = m.sqrt(25)

print("Square root of 25:", result)

Output

Square root of 25: 5.0

Importing `*` in Python

In the above code module, math is not imported, rather just pi has been imported as a variable.
All the functions and constants can be imported using *.

Python3

from math import *

print(pi)

print(factorial(6))

Output

3.141592653589793720

As said above import uses __import__() to search for the module, and if not found, it would raise ImportError

Python3

import mathematics

print(mathematics.pi)

Output:

Traceback (most recent call last): File "C:/Users/GFG/Tuples/xxx.py", line 1, in import mathematicsImportError: No module named 'mathematics'


P

Piyush Doorwar

Import module in Python - GeeksforGeeks (1)

Improve

Please Login to comment...

Import module in Python - GeeksforGeeks (2024)
Top Articles
6 Must-Have Essential Oils for Travel + How To Use Them — Alice Nicholls
About Current Tax Laws
Katie Pavlich Bikini Photos
My E Chart Elliot
Combat level
Ixl Elmoreco.com
Gabriel Kuhn Y Daniel Perry Video
South Park Season 26 Kisscartoon
Air Canada bullish about its prospects as recovery gains steam
Mr Tire Prince Frederick Md 20678
Sportsman Warehouse Cda
Graveguard Set Bloodborne
Mivf Mdcalc
Shooting Games Multiplayer Unblocked
Diablo 3 Metascore
Identogo Brunswick Ga
Uhcs Patient Wallet
Top tips for getting around Buenos Aires
24 Hour Walmart Detroit Mi
Studentvue Columbia Heights
Sony E 18-200mm F3.5-6.3 OSS LE Review
Curtains - Cheap Ready Made Curtains - Deconovo UK
Whitefish Bay Calendar
Craigslist Southern Oregon Coast
Mychart Anmed Health Login
Apple Original Films and Skydance Animation’s highly anticipated “Luck” to premiere globally on Apple TV+ on Friday, August 5
Sullivan County Image Mate
The Tower and Major Arcana Tarot Combinations: What They Mean - Eclectic Witchcraft
Teekay Vop
Jeff Nippard Push Pull Program Pdf
Chime Ssi Payment 2023
Watson 853 White Oval
Truvy Back Office Login
Gillette Craigslist
Taylored Services Hardeeville Sc
Emuaid Max First Aid Ointment 2 Ounce Fake Review Analysis
Winterset Rants And Raves
The value of R in SI units is _____?
Human Unitec International Inc (HMNU) Stock Price History Chart & Technical Analysis Graph - TipRanks.com
Envy Nails Snoqualmie
Skyrim:Elder Knowledge - The Unofficial Elder Scrolls Pages (UESP)
Can You Buy Pedialyte On Food Stamps
Tugboat Information
3496 W Little League Dr San Bernardino Ca 92407
Birmingham City Schools Clever Login
Bill Manser Net Worth
Anthem Bcbs Otc Catalog 2022
My Eschedule Greatpeople Me
Reli Stocktwits
Diesel Technician/Mechanic III - Entry Level - transportation - job employment - craigslist
Southern Blotting: Principle, Steps, Applications | Microbe Online
Latest Posts
Article information

Author: Merrill Bechtelar CPA

Last Updated:

Views: 6364

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Merrill Bechtelar CPA

Birthday: 1996-05-19

Address: Apt. 114 873 White Lodge, Libbyfurt, CA 93006

Phone: +5983010455207

Job: Legacy Representative

Hobby: Blacksmithing, Urban exploration, Sudoku, Slacklining, Creative writing, Community, Letterboxing

Introduction: My name is Merrill Bechtelar CPA, I am a clean, agreeable, glorious, magnificent, witty, enchanting, comfortable person who loves writing and wants to share my knowledge and understanding with you.