yee379
yee379

Reputation: 6762

import python package when module is same name

I have a module blah.time where I do some sanity checks and wrapper functions around normal time and date operations:

import time

def sleep(n):
    time.sleep(n)

When I call sleep, it just throws a maximum recursion error. I'm guessing the namespace is wrong, so I tried using import time as _time, but I still get the same error.

How do I reference the system time module from within my own module in order to prevent this namespace conflict?

Upvotes: 4

Views: 5026

Answers (3)

Ethan Furman
Ethan Furman

Reputation: 69288

What's happening is that your time module is shadowing the system time module. The easiest way around the problem is to rename your module to something besides time.

Upvotes: 0

pelson
pelson

Reputation: 21849

I would read http://docs.python.org/whatsnew/2.5.html#pep-328-absolute-and-relative-imports and then use from __future__ import absolute_import.

HTH

Upvotes: 3

Winston Ewert
Winston Ewert

Reputation: 45089

Add from __future__ import absolute_import as the first line in your file.

This will force all imports to be absolute rather then relative. So import time will import the standard module, to import a local module you'd use from . import foobar

Upvotes: 14

Related Questions