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. 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.

This is the desired behavior and it’s what I get when opening any project in Pycharm except one.

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). from __future__ import print_function would turn print into a function again but it’s Python2 stuff.

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.

Answer

NEW ANSWER

Well, this happened to me just yesterday while I was working on my tkinter project. 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. It worked for me.

Solution Two

Another method I found out is to go to

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). I got this bug while testing some Google API that was in Python 2.7. 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". This is just my theory I may be completely wrong here.

OLD ANSWER

Are You Using macOS? if yes, macOS comes with Python 2 preinstalled. So probably this may be a bug. I suggest going to the bottom right corner and there would be written “Python (Version)”. 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.

If this does not work, then it may be a theme bug, or something to do with PyCharm itself (probably). Reinstalling and clearing the cache should probably work.