简体   繁体   English

使用子模块时,模块可以与包本身具有相同的名称吗?

[英]Can module have the same name as the package itself when submodules are used?

I'm sorry if the title is confusing, I'll try to explain the problem here with more detailed description. 抱歉,标题令人困惑,我将在这里尝试使用更详细的说明来解释该问题。

Currently, I have a package name let's say DummyPackage. 目前,我有一个包名称,比如DummyPackage。 DummyPackage contains three modules with functions, classes, etc. So the directory structure looks like this: DummyPackage包含具有功能,类等的三个模块。因此目录结构如下所示:

project_dir/
    __init__.py
    DummyPackage/
        __init__.py
        Module1/
            __init__.py
            module_x.py
            module_y.py
        Module2/
            __init__.py
            module_z.py

So importing methods from modules looks like this 所以从模块导入方法看起来像这样

from DummyPackage.Module1.module_x import method_x

We are adding new stuff to the project and I would like to create a module, with the name DummyProject, which should be importable like this 我们正在向项目中添加新内容,我想创建一个名为DummyProject的模块,该模块应该像这样可导入

from DummyProject import new_method

I assumed, only adding file DummyPackage.py would be enough, but apparently, it's not. 我以为,仅添加文件DummyPackage.py就足够了,但是显然不是。 I tried to add it to the project_dir/ dir and to DummyPackage/ dir, but neither works. 我试图将其添加到project_dir /目录和DummyPackage /目录中,但均不起作用。

Is it because of name conflict? 是因为名字冲突吗? Is it possible to have a code like this? 是否可以有这样的代码?

import DummyPackage
from DummyPackage.Module1.module_x import method_x

DummyPackage.new_method
method_x

Thanks a lot for your help. 非常感谢你的帮助。

to put my three comments in an answer: 把我的三个评论放在一个答案中:

First let me explain relative imports using the modules you already have, if you wanted to import module_x from module_y you can do this: 首先,让我解释一下您已经使用的模块的相对导入 ,如果您想从module_y导入module_x ,可以这样做:

module_y.py module_y.py

from .module_x import method_x

or similarly in module_z.py 或类似地在module_z.py中

from ..Module1.module_x import method_x

so depending on the location of your DummyProject in the package Intra-package References may be all you need. 因此,根据您DummyProject在软件包中的位置而定,您可能需要的DummyProjectIntra-package References”

As for the second part yes it is possible to have (runnable) code like this: 至于第二部分,可以有这样的(可运行的)代码:

import DummyPackage
from DummyPackage.Module1.module_x import method_x

DummyPackage.new_method
method_x

in this case it looks like you want new_method to be a package level variable. 在这种情况下,您似乎希望new_method是程序包级别的变量。 To quote this great answer : 引用这个好答案

In addition to labeling a directory as a Python package and defining __all__ , __init__.py allows you to define any variable at the package level. 除了将目录标记为Python包并定义__all____init__.py允许您在包级别定义任何变量。

I highly recommend taking a look at the source code for json/__init__.py in the standard library if you want an real world example. 如果您需要真实的示例,我强烈建议您查看标准库中json/__init__.py的源代码。

Or as an example with your setup to be able to import method_x right from the package you would just need to add this to the top level __init__.py : 或以您可以直接从包中导入method_x设置为例,只需将其添加到顶级__init__.py

from .Module1.module_x import method_x

then from any file importing the package you could do this: 然后从导入包的任何文件中可以执行以下操作:

import DummyPackage

DummyPackage.method_x

(Although obviously you would do it for new_method according to where you place DummyProject ) (虽然很明显,你会为这样做new_method根据你的地方DummyProject

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

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