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

删除回忆录丶 提交于 2019-12-10 14:35:42

问题


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?


回答1:


Add at the top of mylib.py:

from __future__ import absolute_import

See Rationale for Absolute Imports.



来源:https://stackoverflow.com/questions/14477040/how-to-import-standard-library-instead-of-same-named-module-in-module-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!