从同级子包导入时未解析的引用 [英] Unresolved reference when importing from sibling sub-package with
问题描述
我有以下类似的目录结构.
I have following similar directory structure.
main.py
pack
|___sub_pack1
|__a.py
|___sub_pack2
|__b.py
现在在 main.py(这是我执行的主程序)中,我正在导入如下
Now inside main.py (which is my main program that I execute) I am importing like follow
from pack.sub_pack1 import a
工作正常.
在a.py里面我像follow一样导入
Inside a.py I am importing like follow
from pack.sub_pack2 import b
此时 pycharm 将上面的代码下划线为红色并通知我未解决的参考包".
At this point pycharm underlines above code as red and notifying me "Unresolved reference pack".
现在我的代码可以正常工作了.我很好奇为什么 pycharm 将其视为错误,我该怎么做才能避免这种情况.
Now my code is working fine which should. I am curious why pycharm considering it as error and what can I do to avoid such thing.
推荐答案
如果您在 PyCharm 中遇到未解决的引用"错误,您可以做的另一件事是:
Another thing you can do if you're having trouble with "Unsolved reference" errors in PyCharm is:
- 右键单击 Python 源目录
- 选择将目录标记为">源根目录"
确保您已为所有 Python 源目录执行此操作.
Make sure you've done this for all your Python source directories.
这篇关于从同级子包导入时未解析的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!