ValueError: attempted relative import beyond top-level package

前端 未结 3 1755
情书的邮戳
情书的邮戳 2020-12-24 10:46

I was playing the the Python\'s import system in order to understand better how it works, and I encountered another problem. I have the following structure

         


        
3条回答
  •  执笔经年
    2020-12-24 11:29

    Python 3 changed the import system so every time you want a module that is around the one you are working, you need relative imports (unless you mess with PYTHONPATH or sys.path).

    The correct usage here should be

    from .subpkg import a
    

    When you are working with IDLE, you have a totally different environment. Therefore, you could add the current location to your path so imports work again.

    try:

    sys.path.insert(0, '')
    

    It might be weird, but it is for a greater good

    PS: If this last thing do not work -- I don't have an IDLE environment right now -- it is probably because the work directory is set wrong.

    Try this answer instead: https://stackoverflow.com/a/17361545/754991

提交回复
热议问题