Import from sibling directory

后端 未结 3 2093
忘了有多久
忘了有多久 2020-11-30 02:14

I have a Python class called \"ClassA\" and another Python class which is supposed to import ClassA which is \"ClassB\". The directory structure is as follows:



        
3条回答
  •  北荒
    北荒 (楼主)
    2020-11-30 02:37

    You really should be using packages. Then MainDir is placed at a point in the file system on sys.path (e.g. .../site-packages), then you can say in ClassB:

    from MainDir.Dir.DirA import ClassA # which is actually a module
    

    You just have to place files named __init__.py in each directory to make it a package hierarchy.

提交回复
热议问题