Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 818
yee379 Avatar asked Dec 03 '22 01:12

yee379


2 Answers

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

like image 82
Winston Ewert Avatar answered Dec 16 '22 14:12

Winston Ewert


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

like image 29
pelson Avatar answered Dec 16 '22 14:12

pelson