Import a python package when the module has the same name

I have a blah.time module where I perform some health checks and wrapper functions around the usual time and date operations:

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

When I call sleep , it just generates the maximum recursion error. I assume the namespace is incorrect, so I tried to use import time as _time , but I still get the same error.

How can I refer to the time module from my own module to prevent this namespace conflict?

+4
source share
3 answers

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

This will result in all imports being absolute rather than relative. Thus, import time imports the standard module to import the local module that you would use from . import foobar from . import foobar

+13
source

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 .

NTN

+3
source

What happens is your time module obscures the systemโ€™s time module. The easiest way to solve the problem is to rename your module to something other than time .

0
source

Source: https://habr.com/ru/post/1437873/


All Articles