Home Coding & Scripting Modules and Imports in Python

Modules and Imports in Python

279
0

When you write Python programs, you don’t have to reinvent the wheel every time. Python provides a way to organize and reuse code using modules.


📝 If You Want to Practice the Code

If you plan to practice the Python code mentioned within this blog post, you’ll need a working Python environment. You can execute the commands in one of the following ways (choose only one option):

👉 Choose whichever environment you’re most comfortable with. If you haven’t set up Python yet, start with one of the installation posts before trying the examples here.


What is a Module?

A module is just a Python file that contains code — functions, classes, or variables — that you can reuse in other programs.

  • If you create a file called math_tools.py with a function inside, that’s a module.
  • Python also comes with a big set of built-in modules (like math, os, and datetime).
  • You can also install extra modules (called packages) with tools like pip.

Example:

# math_tools.py
def add(a, b):
    return a + b

Here, math_tools.py is a module that defines one function.


Importing a Module

To use code from another module, you use the import statement.

Import the whole module

import math

print(math.sqrt(16))  # Output: 4.0

Import a specific function or class

from math import sqrt

print(sqrt(25))  # Output: 5.0

Give a module an alias (nickname)

import math as m

print(m.pi)  # Output: 3.141592653589793

Your Own Modules

If you have math_tools.py in the same folder as your main program, you can import it:

import math_tools

result = math_tools.add(3, 4)
print(result)  # Output: 7

Why Are Imports Useful?

  • They keep code organized and split into logical pieces.
  • They allow you to reuse code instead of copying it everywhere.
  • They give access to Python’s standard library and third-party libraries like requests, numpy, or the Azure SDKs.

Quick Recap

  • A module is just a Python file with reusable code.
  • Use import to bring in a module.
  • You can import everything, just what you need, or rename it with an alias.

👉 Next Step: Try creating your own small module with a function or two, then import it into another Python file. That’s the best way to understand how modules and imports work.

Previous articleGetting Started with Google Colab
Next articleFixing PATH: How to Make Your Shell Use the Right Tool (Windows, macOS, Linux)
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here