PyCharm 中未解决的参考问题

Unresolved reference issue in PyCharm(PyCharm 中未解决的参考问题)

本文介绍了PyCharm 中未解决的参考问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录结构

├── simulate.py
├── src
│   ├── networkAlgorithm.py
│   ├── ...

我可以使用 sys.path.insert() 访问网络模块.

And I can access the network module with sys.path.insert().

import sys
import os.path
sys.path.insert(0, "./src")
from networkAlgorithm import *

但是,pycharm 抱怨它无法访问该模块.如何教 pycham 解析引用?

However, pycharm complains that it cannot access the module. How can I teach pycham to resolve the reference?

推荐答案

手动添加它确实是这样做的一种方法,但有一个更简单的方法,那就是只需告诉 pycharm 您要将 src 文件夹添加为源根目录,然后将源根目录添加到您的 python 路径.

Manually adding it as you have done is indeed one way of doing this, but there is a simpler method, and that is by simply telling pycharm that you want to add the src folder as a source root, and then adding the sources root to your python path.

这样,您不必将内容硬编码到解释器的设置中:

This way, you don't have to hard code things into your interpreter's settings:

  • 添加 src 作为源内容根目录:
  • Add src as a source content root:

  • 然后确保将添加源添加到您的 PYTHONPATH 下:

Preferences ~ Build, Execution, Deployment ~ Console ~ Python Console

  • 现在将解决导入问题:

这样,您可以添加任何您想要的作为源根目录,并且一切都会正常工作.但是,如果您将其取消标记为源根目录,您收到错误消息:

This way, you can add whatever you want as a source root, and things will simply work. If you unmarked it as a source root however, you will get an error:

毕竟不要忘记重新启动.在 PyCharm 菜单中选择:文件 -->使缓存无效/重新启动

After all this don't forget to restart. In PyCharm menu select: File --> Invalidate Caches / Restart

这篇关于PyCharm 中未解决的参考问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:PyCharm 中未解决的参考问题

基础教程推荐