Skip to content

How to import module in Python | Example code

By importing the module you can get access to code from another module in Python. Just use the import statement to import any module in python.

How to import modules in Python?

The python module is nothing but a code of variables, functions, or class defined. This filename becomes the module name. Users can define a module or can use inbuild modules like math modules.

To use the module, you have to import it using the import keyword. Let’s see examples for it:-

Import inbuild python math modules

import math

# Square root of number
sqrValue = math.sqrt(25)
print(sqrValue)

Output: 5.0

Custome module import example

main.py

var = "Hello main file"
num = 9876543210


def greeting(name):
    print("Hello, " + name)

Test.py

import main

main.greeting("John")
print(main.num)

Output:

Hello, John
9876543210

How to import module in Python

Read more ways to import modules:- Python import variable from another file

Do comment if you have any doubts or suggestions on this Python module tutorial.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

2 thoughts on “How to import module in Python | Example code”

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading