简体   繁体   English

当存在具有相同名称的本地模块时,如何在Python中访问标准库模块?

[英]How to access a standard-library module in Python when there is a local module with the same name?

How can a standard-library module (say math) be accessed when a file prog.py is placed in the same directory as a local module with the same name (math.py)? 当文件prog.py与具有相同名称的本地模块(math.py)放在同一目录中时,如何访问标准库模块(比如数学)?

I'm asking this question because I would like to create a package uncertainties that one can use as 我问这个问题是因为我想创建一个可以用作的包uncertainties

import uncertainties
from uncertainties.math import *

Thus, there is a local math module inside the uncertainties directory. 因此,不确定性目录中有一个本地数学模块。 The problem is that I want to access the standard library math module from uncertainties/__init__.py. 问题是我想从不确定性/ __ init__.py访问标准库数学模块。

I prefer not to rename uncertainties.math because this module is precisely intended to replace functions from the math module (with equivalents that handle numerical uncertainties). 我不想重命名不确定性。因为这个模块正是用于替换数学模块中的函数(具有处理数值不确定性的等价物)。

PS: this question pertains to the module I wrote for performing calculations with uncertainties while taking into account correlations between variables. PS:这个问题与我为执行不确定性计算而编写的模块有关,同时考虑了变量之间的相关性。

You are looking for Absolute/Relative imports from PEP 328 , available with 2.5 and upward . 您正在寻找PEP 328的绝对/相对进口, 2.5及以上可供选择。

In Python 2.5, you can switch import's behaviour to absolute imports using a from __future__ import absolute_import directive. 在Python 2.5中,您可以使用from __future__ import absolute_import指令将导入行为切换为绝对导入。 This absolute- import behaviour will become the default in a future version (probably Python 2.7). 这种绝对导入行为将成为未来版本(可能是Python 2.7)的默认行为。 Once absolute imports are the default, import math will always find the standard library's version. 一旦绝对导入是默认导入,导入数学将始终找到标准库的版本。 It's suggested that users should begin using absolute imports as much as possible, so it's preferable to begin writing from pkg import string in your code. 建议用户应尽可能多地开始使用绝对导入,因此最好从代码中的pkg import string开始编写。

Relative imports are still possible by adding a leading period to the module name when using the from ... import form: 使用from ... import表单时,通过向模块名称添加前导句点仍然可以进行相对导入:

from __future__ import absolute_import
# Import uncertainties.math
from . import math as local_math
import math as sys_math

Why can't you rename your local module again? 为什么不能再次重命名本地模块?

Clearly, it's not a "total" replacement, if you still need things from the installed uncertainties . 显然,如果您仍然需要安装的uncertainties ,那么它不是“全部”替代品。

Since it's a partial replacement, you should not give it the same name. 由于它是部分替换,因此您不应该使用相同的名称。

What's different? 有什么不同? What's the same? 有什么相同的? Pick a better name based on that. 根据它选择一个更好的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以导入与标准库同名的本地模块吗? - Can I import a local module with the same name as a standard-library one? 导入与当前模块同名的python标准库 - Importing a python standard library with the same name as the current module 当本地模块屏蔽由外部模块导入的标准库模块时,如何导入外部模块? - How do I import an external module when a local module masks a standard library module imported by the external module? Python 3:当本地模块存在同名时导入核心模块? - Python 3: Import of core module when local module exists with same name? 如何确定模块名称是否是python标准库的一部分 - How to determine if a module name is part of python standard library Python 本地模块名称在第三个包模块中隐藏标准库 - Python local module name shadowing standard lib in third package module 与标准库中的模块同名的模块,并且还需要导入相同的模块 - A module that has the same name as a module in standard library and also need to import the same module 如何导入标准库模块而不是本地目录? - How to import standard library module instead of local directory? 如何在与Python中标准库中的目录相同的目录中导入模块? - How to import a module in the same directory preferred over those in standard library in Python? python本地模块相同 - python local module with same
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM