Nested directories and classes in python libraries

WBOY
Release: 2024-02-06 11:36:14
forward
1260 people have browsed it

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
Copy after login

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"
]
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template