eHow Blog:

User-Submitted Article

How to Use Modules in Python

Modules are a way to separate your Python program into manageable and portable pieces. As you write a Python program, there are often a number of related functions. These can be encapsulated in a module and stored in a separate file. This allows large programs to be more manageable and modular. Also, most third party code is distributed in the form of modules so, if you plan on programming with Python, you should know how to use them.

Difficulty: Moderate
Instructions

    Make Python Modules

  1. Step 1

    Decide on a short and descriptive module name, taking into account that users of your module (including yourself) will have to type this name often. A module name follows the same guidelines as any other identifier in the Python language. It must start with a letter and contain only letters, numbers and the underscore character.

  2. Step 2

    Create a new script file. This script file will hold the code for this module. It should be named "module_name.py". For example if you had a module for dealing with temperature conversions called "temperature", you would create a file called "temperature.py".

  3. Step 3

    Create a file header. In a series of comments, describe what the module is, what functions and classes it contains and how to use them. Include as much information as you can, these module files are often forgotten about even though you may use them in many of your programs.

  4. Step 4

    Write the code. There are no special keywords or statements to define the module, the filename alone is enough. Any code in this file will be part of that module. If you've already written the code in another file, you can cut and paste. Add comments before non-trivial functions describing their function and usage, making as much comment as possible.

  5. Use Python Modules

  6. Step 1

    Import the module into your program prior to using it. The module file has to be either in the same directory or one of your configured module directories. Use the following command:

    "import temperature"

  7. Step 2

    Begin using functions and classes from the module once it is imported.

    "celsius = temperature.fah_to_cel(fahrenheit)"

  8. Step 3

    Import some or all of the module into your current namespace. Typing the module name all the time can be inconvenient. If you use the module name often or it is long or difficult to type, you can import some or all of the functions or classes in the module into your current namespace.

    "# Import the fah_to_cel function
    from temperature import fah_to_cel

    # Import everything from the temperature module
    from temperature import *"

Tips & Warnings
  • Creating a file header isn't necessary, but with any collection of functions and classes you hide away, it's important to document them. If there's a problem later on, you'll need these comments to remind you of all the things in your module.
Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Computers Newsletters
eHow At Home
eHow At Home

Copyright © 1999-2010 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy .   en-US † requires javascript

eHow Computers
eHow_eHow Technology and Electronics