Python Modules

From Wiki
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