PyCharm中未解决的参考问题

编程入门 行业动态 更新时间:2024-10-24 02:30:30
本文介绍了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 path。

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:

&NBSP ;

  • 然后确保将添加源添加到 PYTHONPATH :
  • Then make sure to add add sources to your PYTHONPATH:

  • 现在将解决导入:

这个方式,你可以添加任何你想要的源根,事情就会起作用。但是,如果您将其取消标记为源根,则将收到错误:

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中未解决的参考问题

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

发布评论

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

>www.elefans.com

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