如何在模块路径中导入标准库而不是同名模块 [英] How to import standard library instead of same-named module in module path

查看:165
本文介绍了如何在模块路径中导入标准库而不是同名模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下目录结构

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

main_code .py 只需导入 mylib

from libs import mylib

mylib.py 只需导入时间

import time
print time

现在证明 mylib.py 进口 libs / time.py 内置标准库时间。有没有办法获得'正常'的行为,即 mylib.py 导入内置标准库时间没有更改 time.py ?这是否是正常行为?我是否必须重命名 time.py ?在这个问题上是否有比PEP8更多的风格指南建议?

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?

推荐答案

mylib的顶部添加.py

from __future__ import absolute_import

参见绝对进口的理由

这篇关于如何在模块路径中导入标准库而不是同名模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆