module imports and __init__.py in Python

前端 未结 5 512
执念已碎
执念已碎 2020-12-02 15:49

I am trying to understand what the best practices are with regards to Python\'s (v2.7) import mechanics. I have a project that has started to grow a bit and lets say my code

5条回答
  •  温柔的废话
    2020-12-02 16:02

    Try this:

    package1

    package1.py

    __init__.py

    package2

    test.py

    package1.py:-

    class abc:
        a = 'hello'
        def print_a(self):
        print(a)
    

    __init__.py:-

    from .package1 import abc
    

    package2.py:-

    From package1.package1 import abc
    

    I use these __init__.py to import from a package.

提交回复
热议问题