Importing files other than *.py

M.E.Farmer mefjr75 at hotmail.com
Fri Nov 19 17:11:33 EST 2004


Ed Leafe wrote in message:
> If I have a Python script in a file named "burp.zz", is there any way 
> to import it into another script as you can with files named with the 
> .py extension?
> 	I looked at the imp module, but was not able to make it do what I 
> wanted. Is this possible, or is the py/pyc extension required?
>   Ed Leafe
Hello Ed,
You can import as usuall if you do this:
Save this to your path.

# zz_import.py
import imputil

# define your custom file handler
def handle_zz(filepath, fileinfo, filename):
    sourcefile = open(filepath, 'r')
    data = sourcefile.read()
    sourcefile.close()
    return 0, compile(data, filepath, 'exec'),{}

importer = imputil.ImportManager()
importer.add_suffix('.zz', handle_zz)
importer.install()
# end zz_import.py

Now do this in your code:
import zz_import #now you can import zz files
import burp

If module is not found it will raise an ImportError.

HTH,
    M.E.Famer



More information about the Python-list mailing list