Absolute imports in Python 2.4

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Jun 2 01:37:04 EDT 2009


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.

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

-- 
Gabriel Genellina




More information about the Python-list mailing list