17

I've got a package set up like so:

packagename/
    __init__.py
    numbers.py
    tools.py
    ...other stuff

Now inside tools.py, I'm trying to import the standard library module fractions. However, the fractions module itself imports the numbers module, which is supposed to be the one in the standard library.

The problem is that it tries to import the numbers modules from my package instead (ie my numbers.py is shadowing the stdlib numbers module), and then complains about it, instead of importing the stdlib module.

My question is, is there a workaround so that I can keep the current structure of my package, or is the only solution to rename my own offending module (numbers.py)?

sykora
  • 96,888
  • 11
  • 64
  • 71
  • Sorry, should've read TFQ more thoroughly. Anyway, I'm trying to reproduce the problem now, but I can't. How are you executing the code when the error occurs? I can only reproduce it when I'm running code from inside the package. – Torsten Marek Jan 29 '09 at 15:32

2 Answers2

9

absolute and relative imports can be used since python2.5 (with __future__ import) and seem to be what you're looking for.

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
7

I try to avoid shadowing the standard library. How about renaming your module to "_numbers.py" ?

And of course, you could still do:

import _numbers as numbers
Ryan Ginstrom
  • 13,915
  • 5
  • 45
  • 60