Home » Learn Python » Modules and Packages » Simple Python Module Example

Simple Python Module Example

Here is a very simple example of a Python module.

First, we create a file called mymodule.py containing a simple function called greet.

mymodule.py:
def greet():
    print("Hello")

Now we can use this file in another Python program by ‘importing’ it. Then we can access the greet function via the name of the module (which is just the name of the file without the .py extension).

import mymodule

mymodule.greet()
Hello

Using Aliases to Module Names

You can give imported modules an alias to make them easier to refer to.

import mymodule as mm

mm.greet()

Leave a Reply

Blog at WordPress.com.

%d