Absolute imports in Python 2.4

Discussion in 'Python' started by Steven D'Aprano, Jun 2, 2009.

  1. 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
    Steven D'Aprano, Jun 2, 2009
    #1
    1. Advertising

  2. En Mon, 01 Jun 2009 21:40:26 -0300, Steven D'Aprano
    <> 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
    Gabriel Genellina, Jun 2, 2009
    #2
    1. Advertising

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

    > En Mon, 01 Jun 2009 21:40:26 -0300, Steven D'Aprano
    > <> 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
    Steven D'Aprano, Jun 4, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Cooper

    Absolute Imports and PYTHONPATH

    Richard Cooper, May 5, 2004, in forum: Python
    Replies:
    1
    Views:
    441
    John Roth
    May 6, 2004
  2. Replies:
    0
    Views:
    331
  3. Albert
    Replies:
    4
    Views:
    10,859
    Albert
    Jul 10, 2008
  4. zildjohn01
    Replies:
    0
    Views:
    645
    zildjohn01
    Feb 22, 2011
  5. Victor Hooi
    Replies:
    1
    Views:
    105
    Devin Jeanpierre
    Nov 25, 2013
Loading...

Share This Page