Python Modules

From Wiki
Revision as of 22:21, 1 February 2011 by Scott (talk | contribs) (Created page with '== Search Path == An <code>import</code> statement refers to python files without any path or extension information. To import <code>C:\work\test\my_test.py</code>, you would jus…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Search Path

An import statement refers to python files without any path or extension information. To import C:\work\test\my_test.py, you would just type

import my_test

The interpreter searches in the following locations:

  1. The current directory.
  2. The PYTHONPATH variable.
  3. The standard library

Importing

import time
x = time.localtime()
from time import localtime
x = localtime()
from time import *
x = localtime()

Test Code

Everything in a module file gets executed when the module is first imported. Test code should run only when you explicitly run the module file itself.

if __name__ == '__main__':
    # run test code