简体   繁体   English

在python中导入系统模块

[英]Importing system modules in python

Background: 背景:

I'm writing a symbolic package aimed at pypy for representing expressions and taking derivatives. 我正在写一个针对pypy的符号包,用于表示表达式和获取派生词。 Not working on simplification or anything like that at the moment, just differentiation and evaluation. 目前不进行简化或类似工作,仅进行差异化和评估。

Question: 题:

I'm writing expression classes to represent many of the functions in math (sqrt, log, exp, sin, cos, etc, etc). 我正在编写表达式类来表示数学中的许多功能(sqrt,log,exp,sin,cos等)。 I have named the module that this goes in math to coincide with the module where you natural find said function. 我已将其命名为math要与您自然找到所述函数的模块重合的模块。 I may in the future do the same with cmath and any other module of particular mathematical interest. 将来,我可能会对cmath和其他具有特殊数学意义的模块进行同样的处理。 My module is part of a larger package ldb.algebra so the name math isn't going to collide with anything at the base namespace. 我的模块是更大的软件包ldb.algebra一部分,因此名称math不会与基本名称空间中的任何内容发生冲突。 However, in order to evaluate expressions I'm going to need to import the system math module inside the ldb.algebra.math module, and I can't do this from inside my math module (or any other module at the same level of package for that matter). 但是,为了评估表达式,我将需要在ldb.algebra.math模块内部导入系统math模块,而我无法从我的math模块内部(或与该模块处于同一级别的任何其他模块内部)进行导入包)。 Here's my basic path so far: 到目前为止,这是我的基本路径:

ldb/
    __init__.py - empty
    algebra/
        __init__.py - empty
        math.py

Contents of ldb/algebra/math.py as an example of the problem: 以ldb / algebra / math.py的内容为例:

import math

my_sin = math.sin

Obviously not terribly useful but it shows the idea. 显然不是很有用,但可以说明这一想法。 I'm using python2 (or rather pypy for python2). 我正在使用python2(或者python2的pypy)。 I've found that I can get around this by creating a subpackage, a module under that say sys_math , importing math in that module and exposing the necessary functions but this seems like a lot of work and an ugly mess. 我发现我可以通过创建一个子包,一个sys_math的模块,在该模块中导入math并公开必要的功能来解决此问题,但这看起来像是很多工作并且很麻烦。

So my question is: can I get at the system math module while still having a module called ldb.algebra.math ? 所以我的问题是:我是否仍可以使用名为ldb.algebra.math的模块来获得系统math模块? Perhaps something funny in the __init__.py file like making the module _math and then changing the name in __init__.py somehow, or some special way to import the system module instead of myself. 也许在__init__.py文件中有一些有趣的事情,例如使模块_math然后以某种方式更改__init__.py的名称,或者采用某种特殊的方式代替自己导入系统模块。

My ugly solution so far: 到目前为止,我的丑陋解决方案:

ldb/
    algebra/
        extra_package/
            __init__.py - empty
            sys_math.py

Contents of sys_math.py: sys_math.py的内容:

import math

sin = math.sin
from __future__ import absolute_import

Put that at the top of your module to disable implicit relative imports. 将其放在模块顶部可禁用隐式相对导入。 Then import math will import the built-in math module, and if you want to use relative imports, you have to do them explicitly with syntax like from . import math 然后, import math将导入内置的math模块,如果要使用相对导入,则必须使用from . import math语法来显式地进行它们from . import math from . import math . from . import math

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM