[Python-Dev] __import__ problems

Steven D'Aprano steve at pearwood.info
Fri Nov 28 18:47:24 CET 2008


On Sat, 29 Nov 2008 03:30:49 am Christian Heimes wrote:
...
> May I point you to the two leading underscores? The name '__import__'
> clearly suggests that the function is part of Python's internals. By
> definition all attributes of the form __*__ are not meant to be used
> directly. 

What about __doc__, __name__ and __slots__, to mention just a few?

I believe that __import__() is the recommended way to import a module 
known only at runtime. The docs also don't seem to agree with your 
assertion that __import__ is not to be touched. On the contrary:

"The function is invoked by the import statement. It mainly exists so 
that you can replace it with another function that has a compatible 
interface..."
http://docs.python.org/library/functions.html

Far from saying "Do Not Touch", the docs say "Please, go ahead and 
replace __import__ if needed". If the OP wants "import x.y.z" to load 
module z instead of x, it seems to me that is exactly the problem 
__import__ is designed to allow him to do.


> Any suggestion to change the arguments of __import__() are 
> futile. It's not going to happen unless the feature is required by
> Python's internal import system.

That may very well be true, but I don't know why you're dumping on me. 
It wasn't my suggestion to change __import__. I merely said I could see 
some advantages to it. I spent most of my post telling the OP why I 
thought his arguments were wrong!


-- 
Steven


More information about the Python-Dev mailing list