Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import standard library instead of same-named module in module path

I have the following directory structure

main_code.py
libs/
    __init__.py
    mylib.py
    time.py

with main_code.py just importing mylib:

from libs import mylib

and mylib.py just importing time:

import time
print time

Now it turns out that mylib.py imports libs/time.py and not the built-in standard library time. Is there any way to get the 'normal' behavior, i.e. that mylib.py imports the built-in standard library time, without changing time.py? Is this the 'normal' behavior anyway? Do I have to rename time.py? Are there any style guide recommendations more than PEP8 on that issue?

like image 301
Alex Avatar asked Jan 23 '13 10:01

Alex


1 Answers

Add at the top of mylib.py:

from __future__ import absolute_import

See Rationale for Absolute Imports.

like image 90
jfs Avatar answered Oct 11 '22 02:10

jfs