beyond top level package error in relative import

后端 未结 13 2516
醉梦人生
醉梦人生 2020-11-22 12:50

It seems there are already quite some questions here about relative import in python 3, but after going through many of them I still didn\'t find the answer for my issue. s

13条回答
  •  我在风中等你
    2020-11-22 13:22

    In my case, I had to change to this: Solution 1(more better which depend on current py file path. Easy to deploy) Use pathlib.Path.parents make code cleaner

    import sys
    import os
    import pathlib
    target_path = pathlib.Path(os.path.abspath(__file__)).parents[3]
    sys.path.append(target_path)
    from utils import MultiFileAllowed
    

    Solution 2

    import sys
    import os
    sys.path.append(os.getcwd())
    from utils import MultiFileAllowed
    

提交回复
热议问题