← All lessons

Your own module

Sign in · Sign up

Lesson 7.3

Your own .py file is a module too. The file name without .py is the module name.

# utils.py
def greet(name):
    return f"Hello, {name}!"

# main.py
import utils

print(utils.greet("Mia"))

When importing, you cannot write import utils.py — only import utils. The module is looked for next to the program, so in the example utils.py and main.py sit in the same folder.

Write a program: import the module utils, which the check puts next to your program, call utils.greet with the name person, and print the answer. The call alone prints nothing: greet only returns the text, and print shows it.

Hint

Your own .py file is a module too, and its name is written without the extension.

import utils imports the module, then comes a dot: utils.greet(...). In the brackets is the variable with the name, not text in quotes. The answer has to be printed: print(utils.greet(person)).