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

2

17

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.

Christogram answered 14/12, 2009 at 10:49 Comment(0)
U
22

You are looking for Absolute/Relative imports from PEP 328, available with 2.5 and upward.

In Python 2.5, you can switch import‘s behaviour to absolute imports using a from __future__ import absolute_import directive. This absolute- import behaviour will become the default in a future version (probably 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.

Relative imports are still possible by adding a leading period to the module name when using the from ... import form:

from __future__ import absolute_import
# Import uncertainties.math
from . import math as local_math
import math as sys_math
Unfriended answered 14/12, 2009 at 11:21 Comment(3)
Validated as the answer: this answers the question (even though S. Lott convinced me that I should follow another route :).Christogram
Yeah, I wouldn't use math as a module name either ;)Unfriended
"Probably in 2.7" - That actually never happened. 2.7 users need the absolute_import line if they want this behavior.Bobcat
I
4

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.

Intromission answered 14/12, 2009 at 11:18 Comment(4)
+1: granted, it is not a complete replacement. I will consider renaming the (future) uncertainties.math module. PS: The standard math module is used in uncertainties.py because uncertainties are manipulated through mathematical functions that do not know about uncertainties (the uncertainties.math module could be used, at a speed cost, since it is more general than the standard math module)Christogram
@EOL: You cannot simply replace something as fundamental as "math" with another module of the same name. Period. It's confusing to users. As much as you'd like to provide a smooth, "pin-to-pin" replacement, don't do that. Please don't. Call it "umath" or something that clearly states that it is not math.Intromission
@S. Lott: Yeah, I had thought that import uncertainties.math would be clear, as would from uncertainties import math, but I had failed to see that math.sin() would be confusing. I'll christen it umath in your honor. :)Christogram
@EOL: For an example, look at Python's urllib and urllib2. Rather than try to replace; they provided a new name, clearly separate.Intromission

© 2022 - 2024 — McMap. All rights reserved.