Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Absolute imports in Python 2.4

Reply
Thread Tools

Absolute imports in Python 2.4

 
 
Steven D'Aprano
Guest
Posts: n/a
 
      06-02-2009
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. In Python 2.7, absolute imports will be
the default, and "import sys" will import the standard library module. To
get to the package.sys module, I'll need "from . import sys".

In Python 2.5 and 2.6, relative imports are the default, and package.sys
will shadow the std lib version. I can say:

from __future__ import absolute_import

to use the Python 2.7 behaviour.

What can I do in Python 2.4 to get an absolute import?

I've read PEP 328 and googled, but haven't found any useful advice other
than "well don't do that then". Plenty of pages complaining about
relative imports, but I haven't found any work-arounds others than
renaming the offending module. Are there any other ways around this?

http://www.python.org/dev/peps/pep-0328/



--
Steven
 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      06-02-2009
En Mon, 01 Jun 2009 21:40:26 -0300, Steven D'Aprano
<(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      06-04-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
> <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic imports + relative imports in Python 3 zildjohn01 Python 0 02-22-2011 05:24 PM
Absolute imports? Roy Smith Python 5 01-10-2011 10:13 AM
Imports System.Data or Imports System.Data.SqlClient? Albert ASP .Net 4 07-10-2008 09:00 AM
absolute imports (python-2.5) jUrner@arcor.de Python 0 08-29-2006 01:51 PM
Absolute Imports and PYTHONPATH Richard Cooper Python 1 05-05-2004 11:59 PM



Advertisments