import on modules/files that don't have .py extension

Peter Hansen peter at engcorp.com
Wed Sep 17 11:26:30 EDT 2003


Anthony_Barker wrote:
> 
> Is it possible to import modules/files that have something other than
> the .py extension?

Something like this?

import os, imp

filepath = '/some/long/path/to/file.notpy'
f = open(filepath)
description = ('.notpy', 'r', imp.PY_SOURCE)
module = imp.load_module('__main__', f, filepath, description)

(Slightly edited from some working code.)

-Peter




More information about the Python-list mailing list