Importing system modules in python -
background:
i'm writing symbolic package aimed @ pypy representing expressions , taking derivatives. not working on simplification or @ moment, differentiation , evaluation.
question:
i'm writing expression classes represent many of functions in math (sqrt, log, exp, sin, cos, etc, etc). have named module goes in math coincide module natural find said function. may in future same cmath , other module of particular mathematical interest. module part of larger package ldb.algebra name math isn't going collide @ base namespace. however, in order evaluate expressions i'm going need import system math module inside ldb.algebra.math module, , can't inside math module (or other module @ same level of package matter). here's basic path far:
ldb/ __init__.py - empty algebra/ __init__.py - empty math.py contents of ldb/algebra/math.py example of problem:
import math my_sin = math.sin obviously not terribly useful shows idea. i'm using python2 (or rather pypy python2). i've found can around creating subpackage, module under sys_math, importing math in module , exposing necessary functions seems lot of work , ugly mess.
so question is: can @ system math module while still having module called ldb.algebra.math? perhaps funny in __init__.py file making module _math , changing name in __init__.py somehow, or special way import system module instead of myself.
my ugly solution far:
ldb/ algebra/ extra_package/ __init__.py - empty sys_math.py contents of sys_math.py:
import math sin = math.sin
from __future__ import absolute_import put @ top of module disable implicit relative imports. import math import built-in math module, , if want use relative imports, have them explicitly syntax from . import math.
Comments
Post a Comment