Python Pycharm:打开工作项目时 IDE 上出现“未解析的引用”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20479696/
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
Pycharm: "unresolved reference" error on the IDE when opening a working project
提问by Sergio Ayestarán
Intro
介绍
I have a Python project on a git repository. Everything works ok for most of the team members, we can sync the code and edit it without any problem with Pycharm on different platforms (Windows, Linux)
我在 git 存储库上有一个 Python 项目。对于大多数团队成员来说一切正常,我们可以在不同平台(Windows、Linux)上使用 Pycharm 同步和编辑代码,没有任何问题
The problem
问题
On one of the computers we are getting "Unresolved reference" all over the code on almost every import with the exception of Python's built in libraries (i.e. import datetime is working). This computer is running the Mac version of Pycharm.
在其中一台计算机上,除了 Python 的内置库(即导入日期时间正在运行)之外,几乎每次导入的代码中都出现了“未解析的引用”。这台电脑运行的是 Mac 版的 Pycharm。
The question
问题
Anyone knows how to solve this?, since most of the imports are not recognized code completion and navigation trough goto->declaration and so on is not working. Is there any known issue with the Mac version?
任何人都知道如何解决这个问题?,因为大多数导入都无法识别代码完成和导航槽 goto->declaration 等不起作用。Mac 版本有任何已知问题吗?
Thanks in advance!
提前致谢!
采纳答案by einnocent
The key is to mark your source directory as a source root. Try the following:
关键是将您的源目录标记为源根目录。请尝试以下操作:
- In the Project view, right-click on the Python source directory
- In the dialog menu select Mark Directory As> Source Root
- 在项目视图中,右键单击 Python 源目录
- 在对话框菜单中选择Mark Directory As> Source Root
The folder should now appear blue instead of beige, to indicate it is a Python source folder.
该文件夹现在应该显示为蓝色而不是米色,以表明它是一个 Python 源文件夹。
You can also configure this in PyCharm preferences by doing the following for a project that is already in PyCharm:
您还可以通过对 PyCharm 中已有的项目执行以下操作,在 PyCharm 首选项中进行配置:
- In the Mac toolbar, select PyCharm> Preferences
- In the window that opens, select Project Structurefrom the menu pane on the left
- Select your project in the middle pane, if necessary
- Right-click on your Python source in the right pane and select Sourcesfrom the menu dialog
- 在 Mac 工具栏中,选择PyCharm>首选项
- 在打开的窗口中,从左侧的菜单窗格中选择项目结构
- 如有必要,请在中间窗格中选择您的项目
- 在右窗格中右键单击您的 Python 源,然后从菜单对话框中选择源
回答by A.J.
I also had the problem, and it took me few hours to find the exact solution.
我也遇到了这个问题,我花了几个小时才找到确切的解决方案。
You need to confirm the following things.
您需要确认以下事项。
'django.contrib.staticfiles', is added toINSTALLED_APPSin thesettings.pyfile of your application.The directory with the static contents (for example, images), named
static, resides under the application root.
'django.contrib.staticfiles'被添加到INSTALLED_APPS了在settings.py应用程序的文件。包含静态内容(例如图像)的目录名为
static,位于应用程序根目录下。
Now Do the following
现在执行以下操作
PyCharm > Preferences > Project Settings > Django
PyCharm > 首选项 > 项目设置 > Django
Make sure your Django Project root, Settings.pyand manage.pyscript are well defined in the dialog box.
确保您的Django Project root,Settings.py和manage.py脚本在对话框中定义良好。
You are good to go. Hope this helps.
你已准备好出发。希望这可以帮助。
回答by Simon
I did all the stuff above from einnocent and myildirim but still had to do the following:
我从 einnocent 和 myildirim 完成了上述所有内容,但仍然必须执行以下操作:
close pycharm and manually delete the .idea folder, this deletes everything pycharm knows about the code.
关闭 pycharm 并手动删除 .idea 文件夹,这将删除 pycharm 知道的关于代码的所有内容。
open pycharm, reimport the project
打开pycharm,重新导入项目
the combination of setting the correct source root, restarting python with invaliding cache and deleting the .idea folder / reimporting the pycharm project fixed it for me.
设置正确的源根目录、使用无效缓存重新启动 python 和删除 .idea 文件夹/重新导入 pycharm 项目的组合为我修复了它。
回答by torina
The other reason may be project interpreter. For example, if you are using Python3.x, but the interpreter is for Python2.x You can check here:
另一个原因可能是项目解释器。例如,如果您使用的是 Python3.x,但解释器是针对 Python2.x 的,您可以在这里查看:
File | Settings | Project: 'projectname' | Project Interpreter
档案 | 设置 | 项目:'项目名称' | 项目口译员
回答by emeralddove
PyCharm - Alt-F(ile); Settings; Project Structure; click +Add content root; point your folder containing the package in contention: /home/joker/anaconda3/envs/conda_py27/lib/python2.7/site-packages. I additionally marked as resources (not sure if this is mandatory). Click ok and the reindexing happens. This solvedthe problem for me in PyCharm Professional 2016.2.3
PyCharm - Alt-F(ile); 设置;项目结构;点击+添加内容根目录;指向包含争用包的文件夹:/home/joker/anaconda3/envs/conda_py27/lib/python2.7/site-packages。我另外标记为资源(不确定这是否是强制性的)。单击确定,重新索引就会发生。这在PyCharm Professional 2016.2.3 中为我解决了问题
回答by Abdul Mannan
I faced similar issue with pyspark ( spark 2.1 ) & luigi.
我在 pyspark ( spark 2.1 ) 和 luigi 上遇到了类似的问题。
Failed tries:
失败的尝试:
- Setting PYTHONPATH environment variable
- Invalidate cache & restart pycharm
- Mark directory as Source Root
- 设置 PYTHONPATH 环境变量
- 使缓存无效并重新启动 pycharm
- 将目录标记为源根
Unresolved reference pysparkcould be fixed by adding the spark's python directory as Content Rootin the project, but running the project as luigi task gave the same error.
pyspark可以通过在项目中添加 spark 的 python 目录作为Content Root来修复未解析的引用,但是将项目作为 luigi task 运行会出现相同的错误。
Successfull steps
成功的步骤
- Create an empty
__init__.pyfile in the project Include following lines of code at the top of script
... import sys sys.path.append("/path/to/spark/python") sys.path.append("/path/to/spark/python/lib") ... // import pyspark files after above mentioned lines
__init__.py在项目中创建一个空文件在脚本顶部包含以下代码行
... import sys sys.path.append("/path/to/spark/python") sys.path.append("/path/to/spark/python/lib") ... // import pyspark files after above mentioned lines
and the Unresolved reference error issue was fixed both in pycharm & luigi task.
未解决的参考错误问题在 pycharm 和 luigi 任务中均已修复。
回答by Edward
My ten-penneth - If you're working with virtual environments (have a venvdirectory) make sure it is marked as excluded.
我的十分之一 - 如果您正在使用虚拟环境(有一个venv目录),请确保将其标记为已排除。
Plagiarised from above:
以上抄袭:
- In the Project view, right-click on the
venvdirectory - In the dialog menu select Mark Directory As> Excluded
- 在项目视图中,右键单击
venv目录 - 在对话框菜单中选择将目录标记为>排除
The directory should turn orange...
目录应该变成橙色...
This sometimes happens if you've done a git cleanor for whatever reason had to rebuild your environment and PyCharm hasn't noticed
如果您已经完成git clean或出于任何原因不得不重建您的环境而 PyCharm 没有注意到,有时会发生这种情况

