Python PyCharm 中未解决的参考问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21236824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unresolved reference issue in PyCharm
提问by prosseek
I have a directory structure
我有一个目录结构
├── simulate.py
├── src
│?? ├── networkAlgorithm.py
│?? ├── ...
And I can access the network module with sys.path.insert().
我可以使用sys.path.insert().
import sys
import os.path
sys.path.insert(0, "./src")
from networkAlgorithm import *
However, pycharm complains that it cannot access the module. How can I teach pycham to resolve the reference?
但是,pycharm 抱怨它无法访问该模块。我怎样才能教 pycham 解决参考问题?


采纳答案by Games Brainiac
Manually adding it as you have done isindeed one way of doing this, but there is a simpler method, and that is by simply telling pycharm that you want to add the srcfolder as a source root, and then adding the sources root to your python path.
手动将其添加为你所做的确实这样做的一种方式,但有一个简单的方法,那就是通过简单地告诉pycharm要在添加src文件夹作为源根目录,然后添加源根你的Python小路。
This way, you don't have to hard code things into your interpreter's settings:
这样,您不必将内容硬编码到解释器的设置中:
- Add
srcas a source content root:
- 添加
src为源内容根:


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


- Now imports will be resolved:
- 现在进口将得到解决:


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 willget an error:
通过这种方式,您可以添加任何您想要的作为源根目录,一切都会正常工作。但是,如果您取消将其标记为源根目录,则会出现错误:


回答by prosseek
Normally, $PYTHONPATH is used to teach python interpreter to find necessary modules. PyCharm needs to add the path in Preference.
通常,$PYTHONPATH 用于教 python 解释器找到必要的模块。PyCharm 需要在 Preference 中添加路径。


回答by Ranjeet
Many a times what happens is that the plugin is not installed. e.g.
很多时候会发生插件未安装的情况。例如
If you are developing a django project and you do not have django plugin installed in pyCharm, it says error 'unresolved reference'. Refer: https://www.jetbrains.com/pycharm/help/resolving-references.html
如果你正在开发一个 django 项目并且你没有在 pyCharm 中安装 django 插件,它会显示错误“未解析的引用”。参考:https: //www.jetbrains.com/pycharm/help/resolving-references.html
回答by kishs1991
Please check if you are using the right interpreter that you are supposed to. I was getting error "unresolved reference 'django' " to solve this I changed Project Interpreter (Changed Python 3 to Python 2.7) from project settings: Select Project, go to File -> Settings -> Project: -> Project Interpreter -> Brows and Select correct version or Interpreter (e.g /usr/bin/python2.7).
请检查您是否使用了正确的口译员。我收到错误“未解析的引用'django'”来解决这个问题我从项目设置中更改了项目解释器(将 Python 3 更改为 Python 2.7):选择项目,转到文件 -> 设置 -> 项目: -> 项目解释器 -> 眉毛并选择正确的版本或解释器(例如/usr/bin/python2.7)。
回答by mehdi
After testing all workarounds, i suggest you to take a look at Settings -> Project -> project dependenciesand re-arrange them.
在测试了所有解决方法后,我建议您查看Settings -> Project -> project dependencies并重新安排它们。


回答by Ukr
- check for
__init__.pyfile insrcfolder - add the
srcfolder as a source root - Then make sure to add add sources to your
PYTHONPATH(see above) - in PyCharm menu select: File --> Invalidate Caches / Restart
- 检查
__init__.py文件src夹中的文件 - 将
src文件夹添加为源根 - 然后确保将添加源添加到您的
PYTHONPATH(见上文) - 在 PyCharm 菜单中选择:File --> Invalidate Caches / Restart
回答by Ch_y
Generally, this is a missing package problem, just place the caret at the unresolved reference and press Alt+Enterto reveal the options, then you should know how to solve it.
一般来说,这是一个缺少包的问题,只需将插入符号放在未解析的引用处并按Alt+Enter显示选项,然后您应该知道如何解决它。
回答by AeroHil
If anyone is still looking at this, the accepted answer still works for PyCharm 2016.3 when I tried it. The UI might have changed, but the options are still the same.
如果有人还在看这个,当我尝试它时,接受的答案仍然适用于 PyCharm 2016.3。UI 可能已更改,但选项仍然相同。
ie. Right click on your root folder --> 'Mark Directory As' --> Source Root
IE。右键单击您的根文件夹-->“将目录标记为”--> 源根目录
回答by yunus
Pycharm uses venv. In the venv's console you should install the packages explicitly or go in settings -> project interpreter -> add interpreter -> inherit global site-packages.
Pycharm 使用 venv。在 venv 的控制台中,您应该明确安装软件包或进入settings -> project interpreter -> add interpreter -> inherit global site-packages.
回答by danday74
Install via PyCharm (works with Community Edition). Open up Settings > Project > Project Interpreterthen click the green + icon in the screenshot below. In the 2nd dialogue that opens, enter the package name and click the 'Install Package' button.
通过 PyCharm 安装(适用于社区版)。打开,Settings > Project > Project Interpreter然后单击下面屏幕截图中的绿色 + 图标。在打开的第二个对话框中,输入包名称并单击“安装包”按钮。

