Intro to Modules in Python

Posted by

A Module in python is a file with the py extension that contains functions, classes, and variables.  Modules provide a way to both package and reuse code in different python projects.  A library in Python is a package that contains several modules.  Modules come in three flavors which I’ll write about more below.  The three types of modules are built-in, standard, and third-party. 

Builtin Modules

When you install Python, you are getting some modules that are installed and made available at all times.  So built in functions are included in built in modules and available and are included in Python.  When you run python, you have built in objects created based off the built in modules. To see all built in functions available, run: 

dir(__builtins__)

When using a function without importing it is a good indication that you’re using a built-in object based off a built-in module.A good example is whenever you run print(“hello world”).  This is a function that is executing from a built-in module.

Standard Modules

Standard Modules are installed when you install Python however they aren’t available until you import them. The default location where standard modules live is in your default python installation within the lib directory. Mine is located here:

Displays the module dateitme.py

Let’s explore a standard module.  In my simple scenario, I want to test out the datetime module displayed in the screenshot above.   Launch VS Code, start a new terminal session and execute python to get into the interpreter. 

This displays a python terminal and python command is executed.

Because datetime is a standard module, I’ll need to import it before working with it.  I entered import datetime and pressed enter key.I want to see all the classes, functions, and attributes available.  To see them run dir(datetime)

this displays the commands import datetime and dir(datetime)

How do I know that datetime is a module?   Well, you can run type(datetime) and review the result.

displays the command type(datetime)

You can also run type with any of the items outputted from the dir(datetime).   For Example, I want to look at MAXYEAR:

displays the command, type(datetime.MAXYEAR)

This is an attribute associated with the datetime datatype.   What if I want to look at the  datetime item within datetime by running type(datetime.datetime).

displays the command, type(datetime.datetime)

The return type of type indicates it’s a class.  Python classes contain functions and attributes.  Let’s see what’s available as part of this class by running dir(datetime.datetime)

this displays the command dir(datetime.datetime).

The items that begin and end with __ are built in.  That means they came from one of the built in python data types.  I’m interested in a function unique to the datetime.datetime data type so I want to look at now.   To confirm it’s a function run dir(datetime.datetime.now).

this displays the command type(datetime.datetime.now)

I want to know what this function does and how to use it.  This is where the built-in help function comes in handy. For Example, I’ll run help(datetime.datetime.now)

This displays the command help(datetime.datetime.now)

Finally, I can execute the function by running datetime.datetime.now()

Note: to exit out of the python interpreter, you can hit (Ctrl + Z) or enter quit()

Third Party Modules

Third Party Libraries also called modules are mostly external to the default python install.   3rd Party modules also referred to as packages,  can be installed to your python project by running pip package manager.  PIP is a library that comes with python.  Third-party libraries are super helpful and accelerate python development.  For Example, instead of building my own rest api from scratch, I can import and use Fast API which is a very popular web framework for building out rest API’s.  To see a directory of published python packages, check out the following:  https://pypi.org/

In this example, I’ll try out the wikipedia module. First, I need to install it using pip.  Run: pip install wikipedia

this displays the command pip install wikipedia

After it’s installed, I can import it and try to pull a wikipedia summary down.  It looks like:

This command contains some sample code of wikipedia.page("County Leitrim") and finally prints it to the console by running print(summay)

And it results in the following output

Just like standard modules, you can inspect all the classes and functions included in a module with a mixture of both dir(modulename) and help(modulename.functionname)

Thank You,

Russ Maxwell