Python:导入与函数同名的模块 [英] Python: Importing a module with the same name as a function

查看:60
本文介绍了Python:导入与函数同名的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:第一次就 SE 提出问题.我对 Python 还是很陌生,总体上对编程还不是很熟悉.我已经四处搜索,但没有找到这个问题的答案,非常感谢您的帮助.

For background: First time asking a question on SE. I'm still quite new at Python, and not very experienced with programming in general. 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?

具体来说,我正在使用 Python 符号数学库 sympy 0.7.5,使用 Python 2.7.

To be specific, I am working with the Python symbolic math library, sympy 0.7.5, using Python 2.7.

Sympy 的结构如下:

Sympy has a structure like this:

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

我想要做的是从这个结构中导入 fu.py,这样我就可以调用 TR8 函数了.不过,我的运气并不好.

What I am looking to do is import fu.py from this structure, so that I can call the TR8 function. However, I'm not having much luck.

这有效:

from sympy.simplify.fu import *

TR8(some_expression)

到目前为止,这是我能够访问 TR8 的唯一方式,但我知道这不是推荐的方式.

So far, this is the only way I am able to access TR8, but I know it's not the recommended way.

以下尝试失败:

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'

我不确定,但在我看来,Python 认为我正在尝试导入名为 fu 的函数而不是名为 fu 的模块.同样,当我以这种方式尝试时:

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. Likewise, When I try it this way:

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

这里 Python 似乎认为我在谈论函数 sympy.simplify.simplify,而不是模块 sympy.simplify.

Here Python seems to think I'm talking about the function sympy.simplify.simplify, instead of the module sympy.simplify.

那么,当该模块包含与模块同名的函数时,是否有正确的方法要求 Python 导入模块?

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?

推荐答案

sympy/simplify/__init__.py 包含以下行:

sympy/simplify/__init__.py contains the following line:

from .fu import FU, fu

这通过将 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.

如果你只想要 TR8 功能,你可以导入:

If you just want the TR8 function, you can import that:

from sympy.simplify.fu import TR8

如果你需要fu模块,在sys.modules中保留一个条目:

If you need the fu module, an entry remains in 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.

这篇关于Python:导入与函数同名的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆