Relative imports in Python 3

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

    I was getting this ImportError: attempted relative import with no known parent package

    In my program I was using the file from current path for importing its function.

    from .filename import function
    

    Then I modified the current path (Dot) with package name. Which resolved my issue.

    from package_name.filename import function
    

    I hope the above answer helps you.

提交回复
热议问题