python - 如何阻止 Python 'import' 也导入文件名?
问题描述
我有以下目录结构:
project/
\__ module/
\__ __init__.py
\__ stuff.py
该__init__.py
文件如下所示:
from . import stuff as othername
但是,当我打开 python 交互式解释器并导入模块module
,并调用dir()
模块时,我得到以下结果:
>>> dir(module)
['__builtins__',
'__cached__',
...
'othername',
'stuff']
如您所见,文件的名称stuff
(减去 .py 扩展名)仍然存在。
如果不简单地更改stuff.py
to的名称,othername.py
我将如何导入stuff
as othername
,而不导入stuff
as stuff
?
另外,在旁注中,为同一模块提供别名的最佳方法是什么?
这是应该怎么做的吗...
from . import stuff as othername
aliasname = othername
...还是有另一种被认为是“正确”的方法?
更新
我尝试__all__
在我的文件中手动设置__init__.py
,但文件本身的名称仍包含在导入中。
__init__.py:
from . import stuff as othername
from . import stuff as aliasname
__all__ = [ 'othername', 'aliasname' ]
我已经设法让以下工作,但我不知道它是否会被视为“良好做法”,或者它是否会提供一致的行为:
__init__.py:
from . import stuff as othername
from . import stuff as aliasname
del stuff
解决方案
您无法阻止以真实名称分配模块。毕竟,以下必须设置属性foo
和 bar
包模块对象:
# pkg/__init__.py
from .foo import bar
您当然可以del
添加后的名称(通过import
ing 它):
# pkg/__init__.py
from . import foo as bar
del foo
但请注意:它会导致奇怪的情况,例如
>>> import pkg.foo
>>> from pkg.foo import a
>>> pkg.foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'pkg' has no attribute 'foo'
>>> import pkg.bar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'pkg.bar'
>>> pkg.bar.a is a
True
>>> from pkg.bar import a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'pkg.bar'
当然,如果作为一个模块的状态被pkg.bar
认为是一个实现细节,那么这并不重要,所以没有人会发出import
这样的 s。如果您在不隐藏真实名称的情况下添加别名,这也不太重要。(在您的情况下,为什么不调用lex_c89.py
just c89.py
?反正整个包都是词法分析器......)即使那样,这种隐藏也排除了仅导入您需要的模块的性能优势,因为用户无法指示他们需要什么。
推荐阅读
- sapui5 - 无法在母版页中绑定数据
- c# - 如何在 EF Core 2.1 中定义外键关系
- java - 在 Java 和 JavaFX 中创建二维 ArrayList 时出现 NullPointerException
- asp.net-core - 在 Asp.net Core 2 Identity 中更新密码
- c++ - 设置矩阵 zreos :free(): 无效的下一个大小(快速):0x00000000029c21d0
- php - JSTree:使用 php 和 mysql 创建可扩展的树结构
- c++ - 在 C++ 中为 Windows 编写两因素身份验证器?
- jquery - 禁用 UIKIT 3 文件上传 ajax 请求
- scala - 使用 Circe 将字段添加到 json
- sql - 使用包含所有列的索引是否正确?