简体   繁体   English

打印 [Python] 在 Pycharm 中被视为关键字

[英]Print [Python] seen as a keyword in Pycharm

I'm using Pycharm (2019.1.2 x64) from quite a while and I never changed the standard settings.我使用 Pycharm (2019.1.2 x64) 已经有一段时间了,但我从未更改过标准设置。 The standard settings diplay keywords ( def , class , if ) in orange, pressing tab will auto complete the keyword and add a space, build-in functions ( sum() , map() , print() ) are displayed in purple and pressing tab will auto complete the function name, add brackets and move the cursor inside the brackets.标准设置显示关键字( def , class , if )为橙色,按 Tab 将自动完成关键字并添加空格,内置函数( sum()map()print() )显示为紫色并按选项卡将自动完成 function 名称,添加括号并将 cursor 移动到括号内。

This is the desired behavior and it's what I get when opening any project in Pycharm except one.这是所需的行为,这就是我在 Pycharm 中打开任何项目时得到的结果,除了一个。

In a single project this behavior suddently changed, I did nothing but writing and running code.在一个项目中,这种行为突然发生了变化,我除了编写和运行代码之外什么也没做。 I noticed because tab suddently started indenting by 8 spaces instead of 4, no big deal I changed it back to 4 in the bottom of the IDE, but I also noticed that print was seen as a keyword (so orange with no brackets) instead as a built-in function, just like in Python2 (I never used it and the project's interpreter was Python 3.7).我注意到,因为制表符突然开始缩进 8 个空格而不是 4 个,没什么大不了的,我在 IDE 的底部将它改回 4,但我也注意到print被视为关键字(所以橙色没有括号)而不是一个内置的 function,就像在 Python2 中一样(我从未使用过它,项目的解释器是 Python 3.7)。 from __future__ import print_function would turn print into a function again but it's Python2 stuff. from __future__ import print_function会再次将print变成 function ,但它是 Python2 的东西。

I fixed it by creating a new virtual environment and setting up the interpreter again, so my question is: What causes this unexpected behavior?我通过创建一个新的虚拟环境并再次设置解释器来修复它,所以我的问题是:是什么导致了这种意外行为?

I would like to avoid this when working on my most "serious" projects.在处理我最“严肃”的项目时,我想避免这种情况。 Thanks all.谢谢大家。

NEW ANSWER新答案

Well, this happened to me just yesterday while I was working on my tkinter project.好吧,这发生在我昨天,当时我正在处理我的 tkinter 项目。 It showed tkinter in from tkinter import * "Module not supported in Python 2.7", even though I was using Python 3.8. It showed tkinter in from tkinter import * "Module not supported in Python 2.7", even though I was using Python 3.8.

Solution One解决方案一

To fix this issue, I just simply went to为了解决这个问题,我只是去了

File > Settings > Appearance and Behavior > System Settings > Updates

And updated PyCharm to the latest one.并将 PyCharm 更新到最新版本。 It worked for me.它对我有用。

Solution Two解决方案二

Another method I found out is to go to我发现的另一种方法是 go 到

Press Shift Key Twice > Typing "Restore Default Settings"

Doing this will also restore all the settings partially and fixes the problem for me.这样做也将部分恢复所有设置并为我解决问题。

Why Did This Happen?为什么会这样?

This is due to a bug in older versions of PyCharm (probably).这是由于旧版本的 PyCharm 中的错误(可能)。 I got this bug while testing some Google API that was in Python 2.7.我在测试 Python 2.7 中的一些 Google API 时遇到了这个错误。 If you ever opened a project containing Python 2.7 Interpreter and opened another project that is in Python 3 and above, PyCharm thinks that the code that is written is in Python 2.7 and just formats the guide according to it. If you ever opened a project containing Python 2.7 Interpreter and opened another project that is in Python 3 and above, PyCharm thinks that the code that is written is in Python 2.7 and just formats the guide according to it. Some example can be "f" in print(f"{some_variable_here}") being shown as "Not Supported In Python 2.7".一些示例可能是"f" in print(f"{some_variable_here}") being shown as "Not Supported In Python 2.7". This is just my theory I may be completely wrong here.这只是我的理论,我在这里可能完全错了。

OLD ANSWER旧答案

Are You Using macOS?你在使用 macOS 吗? if yes, macOS comes with Python 2 preinstalled.如果是,macOS 预装了 Python 2。 So probably this may be a bug.所以这可能是一个错误。 I suggest going to the bottom right corner and there would be written "Python (Version)".我建议转到右下角,那里会写“Python(版本)”。 If it is something other than 3 or above (like 2), click on it, and go to "Interpreter Settings" and there, from the drop down menu, select the other Python interpreter that is installed on your device.如果它不是 3 或更高版本(如 2),请单击它,然后将 go 转到“解释器设置”,然后从下拉菜单中 select 安装在您的设备上的其他 ZA7F5F35426B96378277 解释器。

If this does not work, then it may be a theme bug, or something to do with PyCharm itself (probably).如果这不起作用,则可能是主题错误,或者与 PyCharm 本身有关(可能)。 Reinstalling and clearing the cache should probably work.重新安装和清除缓存应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM