Home  >  Article  >  Backend Development  >  Nested directories and classes in python libraries

Nested directories and classes in python libraries

WBOY
WBOYforward
2024-02-06 11:36:141261browse

python 库中的嵌套目录和类

Question content

Efficient import

There are several nested directories in this library I'm building, some nested directories have no files but others don't, or each directory has one or more class files.

The library is basically ported from another language and I have to keep the structure the same.

I'm looking for code organization and module access without duplication in import statements

mylib
├── foo
│   ├── bar
│      └── baz.py (class baz) 
├
test
    ── foo
       ├── bar
           ── test_baz.py

The problem I have is the duplication in the import statement

from mylib.foo.bar.baz import baz

Is there any way to avoid .baz appearing in the import statement?

Tried (without any real success)

In the __init__ file, I've tried this without any real effect.

import baz.baz import Baz

__all__ = [
  "Baz"
]

Correct answer


You can use from .baz import Baz# in mylib/foo/bar/__init__.py ##.

The above is the detailed content of Nested directories and classes in python libraries. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete