Relative imports in Python

后端 未结 1 1452
囚心锁ツ
囚心锁ツ 2020-12-02 18:38

Hey all -- I am pulling my hair out with relative imports in Python. I\'ve read the documentation 30 times and numerous posts here on SO and other forums -- still doesn\'t

相关标签:
1条回答
  • 2020-12-02 19:29

    Nevermind, I solved it:

    src/
        main.py
        mod/
            __init__.py
            components/
                __init__.py
                expander.py
                language_id.py
            utilities/
                __init__.py
                functions.py
    

    main.py then refers to the subpackages as:

    from mod.components.expander import *
    from mod.utilities.functions import *
    

    expander.py and language_id.py have access to functions.py with:

    from ..utilities.functions import *
    

    But the interesting thing is that I had a text file inside the components directory that expander.py uses. However, at runtime it couldn't locate the file even though it was in the same directory. I moved the text file to the same directory as main.py and it worked. Seems counter-intuitive.

    0 讨论(0)
提交回复
热议问题