Absolute imports in Python 2.4

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Jun 4 02:21:32 CEST 2009


On Tue, 02 Jun 2009 02:37:04 -0300, Gabriel Genellina wrote:

> En Mon, 01 Jun 2009 21:40:26 -0300, Steven D'Aprano
> <steve at remove-this-cybersource.com.au> escribió:
> 
>> I have a package which includes a module which shadows a module in the
>> standard library. For example:
>>
>> package
>> +-- __init__.py
>> +-- ham.py
>> +-- spam.py
>> +-- sys.py
>>
>> Inside that package, I want to import the standard library sys. In
>> other words, I want an absolute import. [...] What can I do in Python
>> 2.4 to get an absolute import?
> 
> sys = __import__("sys", {})
> 
> The import statement uses the global namespace to determine which
> package it is called on; if you pass an empty namespace, it cannot infer
> package information.

Oh that's very cunning! Nice trick.


> Anyway, the best move would be to rename the offending module...

I agree. But it's nice to know the work-around if I need it.



-- 
Steven



More information about the Python-list mailing list