relative import in pycharm 2018 does not work like relative import in python 3.6

后端 未结 2 1148
名媛妹妹
名媛妹妹 2020-12-15 05:41

I have read endless discussions on relative import in python, I think that one of the reasons it is so confusing is that it changes fro one Python version to another (my ver

相关标签:
2条回答
  • 2020-12-15 06:17

    Mark subfolder as Source Root with right-click in the project tree -> Mark directory as ... -> Sources Root. PyCharm adds all Source Roots to PYTHONPATH by default so the issue should be resolved

    The problem is PyCharm doesn't know you are going to execute BB.py directly, e.g. let's say you have main.py in the root with from subfolder import BB. Calling python main.py will raise ModuleNotFoundError: No module named 'AA' (make sure to use Python 3 to avoid implicit relative imports from Python 2).

    Hope it makes sense and I didn't miss anything.

    0 讨论(0)
  • 2020-12-15 06:39

    You can get both python and pycharm to agree by using

    from subfolder.AA import myfunc
    

    However, according to here, from .AA import myfunc appears to be the correct syntax. But idk why it's not working.

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