beyond top level package error in relative import

后端 未结 13 2443
醉梦人生
醉梦人生 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:18

    None of these solutions worked for me in 3.6, with a folder structure like:

    package1/
        subpackage1/
            module1.py
    package2/
        subpackage2/
            module2.py
    

    My goal was to import from module1 into module2. What finally worked for me was, oddly enough:

    import sys
    sys.path.append(".")
    

    Note the single dot as opposed to the two-dot solutions mentioned so far.


    Edit: The following helped clarify this for me:

    import os
    print (os.getcwd())
    

    In my case, the working directory was (unexpectedly) the root of the project.

提交回复
热议问题