PyCharm 中未解决的参考问题

编程入门 行业动态 更新时间:2024-10-24 06:29:45
本文介绍了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 中未解决的参考问题

本文发布于:2023-11-07 06:34:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565782.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中未   PyCharm

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!