How can I import a py script by its absolute path name?

Peter Hansen peter at engcorp.com
Thu Jul 14 10:53:22 EDT 2005


Jesse Noller wrote:
> for f in os.listdir(os.path.abspath(libdir)):
>     module_name = f.strip('.py')
>     import module_name
> 
> Obviously, this throws:
> ImportError: No module named module_name
> 
> Is there some way to do this?

Use the __import__ builtin function.



More information about the Python-list mailing list