从Python标准库强制导入模块而不是PYTHONPATH默认值 [英] Force import module from Python standard library instead of PYTHONPATH default

查看:341
本文介绍了从Python标准库强制导入模块而不是PYTHONPATH默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的PYTHONPATH中的一个目录中有一个自定义模块,其名称与其中一个标准库模块相同,因此当我 import module_name 时,该模块加载。如果我想使用原始标准库模块,有没有办法强制Python从标准库而不是从PYTHONPATH目录导入,除了重命名自定义模块并更改每个引用以指向新名称?

I have a custom module in one of the directories in my PYTHONPATH with the same name as one of the standard library modules, so that when I import module_name, that module gets loaded. If I want to use the original standard library module, is there any way to force Python to import from the standard library rather than from the PYTHONPATH directory, short of renaming the custom module and changing every reference to point to the new name?

推荐答案

理想的解决方案是将模块重命名为非标准库中的模块。

The ideal solution would be to rename your module to something not in the standard library.

您还可以切换绝对导入如果您使用的是Python 2.5 +:

You can also switch absolute imports on if you're on Python 2.5+:

from __future__ import absolute_import

这篇关于从Python标准库强制导入模块而不是PYTHONPATH默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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