How to do relative imports in Python?

前端 未结 15 2854
情深已故
情深已故 2020-11-21 04:47

Imagine this directory structure:

app/
   __init__.py
   sub1/
      __init__.py
      mod1.py
   sub2/
      __init__.py
      mod2.py

I\'

15条回答
  •  心在旅途
    2020-11-21 05:25

    As @EvgeniSergeev says in the comments to the OP, you can import code from a .py file at an arbitrary location with:

    import imp
    
    foo = imp.load_source('module.name', '/path/to/file.py')
    foo.MyClass()
    

    This is taken from this SO answer.

提交回复
热议问题