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)?
I'm asking this question because I would like to create a package uncertainties
that one can use as
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.
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.
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
.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.
You are looking for Absolute/Relative imports from PEP 328, available with 2.5 and upward.
Relative imports are still possible by adding a leading period to the module name when using the from ... import form: