Relative imports in Python 3

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

    Whenever you get an error produced by the interpreter or compiler read it carefully. The error message says all about the cause of the encountered error in a nutshell. You are having an ImportError in this case. As you’ve mentioned, you are trying to import a user-defined function from another file within the same directory.

    Let me give you an example: https://kodlogs.com/38948/importerror-attempted-relative-import-with-no-known-parent-package

    I also learnt that from there, Hope that helps.

提交回复
热议问题