Relative imports in Python 3

前端 未结 16 1081
误落风尘
误落风尘 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:12

    Moving the file from which you are importing to an outside directory helps.
    This is extra useful when your main file makes any other files in its own directory.
    Ex:
    Before:
    Project
    |---dir1
    |-------main.py
    |-------module1.py
    After:
    Project
    |---module1.py
    |---dir1
    |-------main.py

提交回复
热议问题