Python – Import Modules

Modules

With Python working directly in the interpreter is a quick way to gain experience and work through tutorials. However, once the interpreter is shut down you lose module references, functions, and variables that you have created. Therefore it is beneficial to create scripts through text editors like Sublime Text rather than working directly in the interpreter. Additionally, you may want to link multiple files for easier maintenance with larger applications or reference a function that you have created in a past project.

Python allows you to create a module which is a file with an extension “.py”. The file contains the definitions and statements that you will run from the console line by executing the “python” command and filename+extension (ie: python filename.py).

When writing an application with your text editor you would import the module at the header and then reference either built-in or custom functions and variables within that file. Understanding how to reference files and its associated definitions is important when expanding your programming knowledge through reference material.

For instance, if you wanted to reference the Python Standard Library you would use the import statement: import sys

You can use dir() to list the names that you currently have defined in your script. If you wanted to list all of the built-in functions and variables you would call dir(bulletins) after your import statement.

For a more complete reference of python modules see The Python Standard Library reference.

**This article is written for Python 3.6

Console Line Input / Output >>

%d bloggers like this: