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 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中未解决的参考问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 23:22