简体   繁体   English

python模块层次结构命名约定

[英]python modules hierarchy naming convention

I'd like to have modules/packages structure like following: 我想要具有如下的模块/包结构:

/__init__.py
/mymodule.py
/mymodule/
/mymodule/__init__.py
/mymodule/submodule.py

And then use modules like: 然后使用如下模块:

import mymodule
import mymodule.submodule

But it seems like file " mymodule.py " conflicts with " mymodule " directory. 但是似乎文件“ mymodule.py ”与“ mymodule ”目录冲突。

What's the correct naming convention here? 这里正确的命名约定是什么?

If you want to make a package, you have to understand how Python translates filenames to module names. 如果要制作软件包,则必须了解Python如何将文件名转换为模块名。

The file mymodule.py will be available as the mymodule , assuming the interpreter finds it in a directory in the Python search path. 假设解释器在Python搜索路径的目录中找到文件mymodule.py它将作为mymodule可用。 If you're on a case-insensitive filesystem, it might also be importable with different capitalization (but you should avoid using such system-dependent behavior). 如果您使用的是不区分大小写的文件系统,则也可以使用不同的大小写将其导入(但应避免使用这种依赖于系统的行为)。

A package is a directory with an __init__.py file in it. 包是其中包含__init__.py文件的目录。 There's been some movement recently to allow packages without those files, but I'm going to ignore that less-common case for this answer. 最近发生了一些移动,允许没有这些文件的软件包,但是对于该答案,我将忽略这种不常见的情况。 A package becomes a module inside Python, with its code coming from the __init__.py file. 一个包成为Python内部的一个模块,其代码来自__init__.py文件。 So the file mypackage/__init__.py can be imported as mypackage . 因此,文件mypackage/__init__.py可以作为mypackage导入。

There's no meaning to an __init__.py file directly in the Python search path (well, I suppose you could import it an an __init__ module, but this is probably a bad idea). 直接在Python搜索路径中的__init__.py文件没有任何意义(嗯,我想您可以将其导入__init__模块,但这可能不是一个好主意)。

So, for your situation, here's the appropriate filesystem layout: 因此,根据您的情况,以下是适当的文件系统布局:

toplevel/
    mymodule/
        __init__.py     # put code here for mymodule
        submodule.py    # put code here for mymodule.submodule

Only the toplevel folder should be in the Python search path. 只有toplevel文件夹应该在Python搜索路径中。

You are dealing with a package . 您正在处理包裹 The package structure you should have is: 您应该具有的包结构是:

/some-parent-directory # This needs to be on sys.path
    /mymodule  # This is not really a module - it's a package
        __init__.py  # import mymodule
        # init is loaded when you `import mymodule` or anything below it
        some.py  # import mymodule.some
        implementation.py  # import mymodule.implementation
        files.py  # import mymodule.files
        /submodule
            __init__.py  # import mymodule.submodule
            # init is loaded when you `import mymodule.submodule` or anything below it
            submodule_impl.py  # import mymodule.submodule.submodule_impl
            goes.py  # import mymodule.submodule.goes
            here.py  # import mymodule.submodule.here

As long as the parent directory is on sys.path you will be able to call import mymodule or from mymodule.submodule import something without issue. 只要目录位于sys.path您就可以调用import mymodulefrom mymodule.submodule import something而不会出现问题。

If you want something to be available from the root level of a package (ie from mymodule import SomeItem or from a sub-package from mymodule.submodule import AnotherItem ) then you can import it into the appropriate __init__.py file. 如果你想要的东西,可购自包的根级别(即from mymodule import SomeItem或子包from mymodule.submodule import AnotherItem ),那么你可以将它导入合适的__init__.py文件。

So, for example, let's say you wanted the class CustomClass defined in the submodule_impl.py module to be importable directly from submodule . 因此,举例来说,假设您希望可以直接从submodule导入submodule_impl.py模块中定义的CustomClass类。 Your submodule/__init__.py would have to contain the following: 您的submodule/__init__.py必须包含以下内容:

from .submodule_impl import CustomClass

Then you would be able to import CustomClass directly from submodule (ie from mymodule.submodule import CustomClass ) 然后,您将能够直接从submodule导入CustomClass (即, from mymodule.submodule import CustomClass

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

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