How To/python/

How to import module from path

Add new path to system paths

You can add new path to lists of paths where python have to look for modules.

import sys
sys.path.insert(0, 'path/to/my/dir/with/modules')
import mymodule

Use imp module

imp module gives you acces to some functions you can use to load modules.

import sys
import imp
 
mymodule = imp.load_source('mymodulename', 'path/to/my/module.py')
 
print mymodule
print sys.modules['mymodulename']

see also:

imp.load_compiled(name, pathname[, file])
imp.find_module(name[, path])
imp.load_module(name, file, pathname, description)