Defining and Importing Python Modules

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Objectives:

  • Defining modules
  • Using the import statement

Note: For this exercise involving modules, it is critically important to make sure you are running Python in a proper environment. You may need to check the value of sys.path if you can't get import statements to work. Ask for assistance if everything seems broken.

Before starting this exercise, first restart your Python interpreter session. If using IDLE, click on the shell window and look for a menu option "Shell > Restart Shell". You should get a message like this:

>>> ##################== RESTART ##################==
>>>

If you are using Unix, simply exit Python and restart the interpreter.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ErrorandExceptionHandlingGroup -.-> python/catching_exceptions("`Catching Exceptions`") python/FileHandlingGroup -.-> python/file_opening_closing("`Opening and Closing Files`") python/BasicConceptsGroup -.-> python/python_shell("`Python Shell`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-132395{{"`Defining and Importing Python Modules`"}} python/with_statement -.-> lab-132395{{"`Defining and Importing Python Modules`"}} python/variables_data_types -.-> lab-132395{{"`Defining and Importing Python Modules`"}} python/numeric_types -.-> lab-132395{{"`Defining and Importing Python Modules`"}} python/type_conversion -.-> lab-132395{{"`Defining and Importing Python Modules`"}} python/conditional_statements -.-> lab-132395{{"`Defining and Importing Python Modules`"}} python/for_loops -.-> lab-132395{{"`Defining and Importing Python Modules`"}} python/lists -.-> lab-132395{{"`Defining and Importing Python Modules`"}} python/tuples -.-> lab-132395{{"`Defining and Importing Python Modules`"}} python/function_definition -.-> lab-132395{{"`Defining and Importing Python Modules`"}} python/importing_modules -.-> lab-132395{{"`Defining and Importing Python Modules`"}} python/using_packages -.-> lab-132395{{"`Defining and Importing Python Modules`"}} python/catching_exceptions -.-> lab-132395{{"`Defining and Importing Python Modules`"}} python/file_opening_closing -.-> lab-132395{{"`Defining and Importing Python Modules`"}} python/python_shell -.-> lab-132395{{"`Defining and Importing Python Modules`"}} python/build_in_functions -.-> lab-132395{{"`Defining and Importing Python Modules`"}} end

Using the import statement

In previous exercises, you wrote two programs pcost.py and stock.py. Use the import statement to load these programs and use their functionality:

>>> import pcost
44671.15
>>> pcost.portfolio_cost('portfolio2.dat')
19908.75
>>> from stock import Stock
>>> s = Stock('GOOG', 100, 490.10)
>>> s.name
'GOOG'
>>> s.cost()
49010.0
>>>

If you can't get the above statements to work, you might have placed your programs in a funny directory. Make sure you are running Python in the same directory as your files or that the directory is included on sys.path.

Main Module

In your pcost.py program, the last statement called a function and printed out the result. Modify the program so that this step only occurs if the program is run as the main program. Now, try running the program two ways:

First, run the program as main:

python3 pcost.py
44671.15

Next, run the program as a library import. You should not see any output.

>>> import pcost
>>>

Summary

Congratulations! You have completed the Defining Modules lab. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like