importing a module in nested packages

后端 未结 3 1508
我寻月下人不归
我寻月下人不归 2020-12-05 17:35

This is a python newbie question:

I have the following directory structure:

test
 -- test_file.py
a
 -- b
   -- module.py    

wher

3条回答
  •  无人及你
    2020-12-05 18:03

    The first thing to do would be to quickly browse the official docs on this.

    To make a directory a package, you'll have to add a __init__.py file. This means that you'll have such a file in the a and b directories. Then you can directly do an

    import a.b.module
    

    But you'll have to refer to it as a.b.module which is tedious so you can use the as form of the import like so

    import a.b.module as mod #shorter name
    

    and refer to it as mod.

    Then you can instantiate things inside mod using the regular conventions like mod.shape().

    There are a few other subtleties. Please go through the docs for details.

提交回复
热议问题