why import plugins used exec instead of __import__?

Alexander Belchenko bialix at ukr.net
Tue Jun 17 06:41:13 BST 2008


Ah, I found it. __import__ allows to import any file even with non-valid python name,
while exec "import foo-bar" will produce Syntax Error.

Sorry for noise email.

Alexander Belchenko пишет:
> Some people and me discussed various plugin systems in other python 
> forum and pointed at bzrlib implementation (bzrlib/plugin.py) as good 
> example. Some people said they dislike using exec or eval in their 
> programs. So bzrlib approach looks not the best. Specifically in 
> bzrlib/plugin.py there is following code:
> 
>     for name in plugin_names:
>         try:
>             exec "import bzrlib.plugins.%s" % name in {}
>         except KeyboardInterrupt:
>             raise
> 
> Why there used exec and not __import__? IIUC import statement under the 
> hood calls __import__,
> so actually there is no big difference in functionality.
> But instead exec must parse python code and then execute corresponding 
> bytecode.
> 
> I don't remember when discussion about such change took place, and
> can't find it in archives.
> Can anyone comment this decision?
> Thank you.
> 
> 
> 




More information about the bazaar mailing list