Relative imports in Python 3

前端 未结 16 1085
误落风尘
误落风尘 2020-11-21 06:42

I want to import a function from another file in the same directory.

Sometimes it works for me with from .mymodule import myfunction but sometimes I get

16条回答
  •  遥遥无期
    2020-11-21 07:11

    If none of the above worked for you, you can specify the module explicitly.

    Directory:

    ├── Project
    │     ├── Dir
    │     │    ├── __init__.py
    │     │    ├── module.py
    │     │    └── standalone.py
    

    Solution:

    #in standalone.py
    from Project.Dir.module import ...
    

    module - the module to be imported

提交回复
热议问题