Python Importing Modules

PythonPythonBeginner
Practice Now

Introduction

Welcome to the enchanted castle of Codemara, home to the wise and revered Gatekeeper of Pythonic Knowledge. As an aspiring wizard of code, your quest is to master the arcane art of module importing, an essential skill for any Python sorcerer worth their wand. The castle contains many secret chambers where magic spells (modules) are inscribed in ancient books (files). To use these spells in your incantations (programs), you must learn to navigate the castle’s labyrinthine corridors and invoke the correct import incantations.

Your goal is to earn the Gatekeeper’s respect by demonstrating mastery over importing modules, both standard and custom, and utilizing their arcane functions to cast powerful spells. Only then will you be awarded the Key of Modular Knowledge and earn your place among the Python wizards of Codemara.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") subgraph Lab Skills python/importing_modules -.-> lab-271560{{"`Python Importing Modules`"}} end

Importing Standard Modules

In this step, you'll learn how to invoke the standard modules that come with Python's extensive library. Think of these as the common spells shared among all wizards within the castle of Codemara.

Open a file named ~/project/standard_spellbook.py. In this file, you will import the math module to calculate the square root of a number:

import math

number_to_calculate = 16
sqrt_value = math.sqrt(number_to_calculate)
print(f"The square root of {number_to_calculate} is {sqrt_value}")

To run your spell, enter the following command in your terminal:

python ~/project/standard_spellbook.py

The expected output should show the square root of 16:

The square root of 16 is 4.0

Creating and Importing Custom Modules

The Gatekeeper has been impressed by your ability to harness the power of standard modules. Now, he challenges you to write your own spell (custom module) and import it.

Open a file named ~/project/custom_spellbook.py. This will be your custom module. Define a new spell (function) that can cast a mysterious echo throughout the codemarble halls:

## custom_spellbook.py
def echo_spell(message):
    return message + "... " + message

Now, in a separate file named ~/project/apprentice_wizard.py, use the following code to invoke your new spell:

## apprentice_wizard.py
from custom_spellbook import echo_spell

message_to_echo = "Hello Codemara"
echoed_message = echo_spell(message_to_echo)
print(echoed_message)

Run your apprentice wizard’s script:

python ~/project/apprentice_wizard.py

You should hear the echo resonating:

Hello Codemara... Hello Codemara

Handling Module Import Errors

As any powerful wizard knows, casting spells can go awry when the incantations are flawed. The Gatekeeper now teaches you how to handle errors that might occur during the importing of a module.

Open a file ~/project/faulty_spellbook.py with a simple error within the import statement:

## Not a real Python module - expect error
try:
    import time_travel
except ImportError:
    print("The time_travel spell doesn't exist in this dimension.")

Run the script in your terminal:

python ~/project/faulty_spellbook.py

The output will wisely inform you of the situation:

The time_travel spell doesn't exist in this dimension.

Summary

In this lab, you embarked on a journey through the mystic halls of the Codemara castle, learning the foundational skill of importing modules. You were introduced to the concept by invoking standard modules, advanced your knowledge by crafting and importing your own custom modules, and exercised caution with the handling of import errors - a crucial skill for gracefully managing misfires in the magical realm of coding. With each step, you have gained proficiency and are now well on your way to becoming a proficient wizard of Python. May the Key of Modular Knowledge guide you through the rest of your enchanted programming adventures.

Other Python Tutorials you may like