简体   繁体   English

Python:导入与函数同名的模块

[英]Python: Importing a module with the same name as a function

For background: First time asking a question on SE. 背景:第一次在SE上提问。 I'm still quite new at Python, and not very experienced with programming in general. 我在Python方面还很陌生,而且在编程方面也不是很有经验。 I've searched around but I haven't found an answer to this question, and I'd greatly appreciate your help. 我已经四处寻找,但我没有找到这个问题的答案,我非常感谢你的帮助。

My question is: How can I import a module which has the same name as a function? 我的问题是:如何导入与函数同名的模块?

To be specific, I am working with the Python symbolic math library, sympy 0.7.5, using Python 2.7. 具体来说,我正在使用Python 2.7符号数学库,sympy 0.7.5。

Sympy has a structure like this: Sympy有这样的结构:

sympy
 |
 +-- __init__.py
 +-- simplify
       |
       +-- __init__.py
       +-- simplify.py
       |      |       
       |      +-- def simplify(...)
       |
       +-- fu.py
            |
            +-- def TR8(...)
            +-- def fu(...)

What I am looking to do is import fu.py from this structure, so that I can call the TR8 function. 我要做的是从这个结构导入fu.py,以便我可以调用TR8函数。 However, I'm not having much luck. 但是,我没有太多运气。

This works: 这有效:

from sympy.simplify.fu import *

TR8(some_expression)

So far, this is the only way I am able to access TR8, but I know it's not the recommended way. 到目前为止,这是我能够访问TR8的唯一方法,但我知道这不是推荐的方式。

The following attempts fail: 以下尝试失败:

from sympy.simplify import fu

fu.TR8(some_expression)
>>AttributeError: 'function' object has no attribute 'TR8'

from sympy import fu

fu.TR8(some_expression)
>>AttributeError: 'function' object has no attribute 'TR8'

I'm not certain, but it seems to me like Python thinks I'm trying to import the function called fu instead of the module called fu. 我不确定,但在我看来,Python似乎认为我正在尝试导入名为fu的函数而不是名为fu的模块。 Likewise, When I try it this way: 同样,当我这样尝试时:

import sympy.simplify.fu as fu
>>AttributeError: 'function' object has no attribute 'fu'

Here Python seems to think I'm talking about the function sympy.simplify.simplify, instead of the module sympy.simplify. 这里Python似乎认为我在谈论函数sympy.simplify.simplify,而不是模块sympy.simplify。

So is there a proper way to ask Python to import a module, when that module contains a function with the same name as the module? 当模块包含与模块同名的函数时,有没有正确的方法让Python导入模块?

sympy/simplify/__init__.py contains the following line: sympy/simplify/__init__.py包含以下行:

from .fu import FU, fu

This hides the fu module by assigning the fu function where you would expect the module to go, blocking most ways to import it. 这通过将fu函数分配到您期望模块去的位置来隐藏fu模块,阻止大多数导入它的方法。

If you just want the TR8 function, you can import that: 如果您只想要TR8功能,可以导入:

from sympy.simplify.fu import TR8

If you need the fu module, an entry remains in sys.modules : 如果需要fu模块,则条目仍保留在sys.modules

import sympy.simplify.fu
import sys

fu_module = sys.modules['sympy.simplify.fu']

It's ugly, but it should work. 这很难看,但应该有用。 As far as I know, this is the simplest way to get at the module itself. 据我所知,这是获取模块本身的最简单方法。

You can also use a namespace for the module. 您还可以为模块使用命名空间。

from simpy.simplify import fu as fu_module

That should work just fine. 这应该工作得很好。

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

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