python 2.7 syntax errors in python3 code

Hello,

I have some python3 code with "Extended Iterable Unpacking". e.g. calling a function like this: foo(*args). This was not allowed in python2.
pycharm now shows me an error in the editor that python 2.7 does not support this syntax.
How can I change the editor to use python3 syntax?

Thank you
6
21 comments
Do you have a Python 3 interpreter selected in Settings -> Project interpreter?

Have you looked at Settings -> Inspections -> Python -> 'Code compatibility inspection'?
25
The interpreter was the right one but I could not find the 'Code compatibility inspection' until now. So I was able to resolved my problem.

Thank you
1

I turned off code compatibility inspection and my configuration is using a Python 3.7 interpreter. The PyCharm editor is still saying "Type annotations are not supported in Python 2" and "Python Version 2.7 does not support a 'F' prefix"

2

Hi Dspells,

"Type annotations are not supported in Python 2" - try updating to the latest PyCharm version. When using python 2.7 and typing module it should be just fine.

"Python Version 2.7 does not support a 'F' prefix" - please vote for https://youtrack.jetbrains.com/issue/PY-31537 and follow for updates.

0

Thanks Sergey,

Version 2018.3.1 seems to be working much better.

0

This still happens in PyCharm 2019.1, I have disabled code compatibility inspections but still get an error on any python 3 code (e.g. f-strings, underscore in numbers 100_000, etc.), a workaround is to disable/unset the interpreter and setting it again

0

I am having these problems again. Including:

  1. Type annotation errors
  2. f-strings (Since I'm using Python 3.7 I don't think that PY-31537 should be a problem)
  3. Can't resolve references to 'str', 'len' and int
  4. PyCharm can't find some modules like 'logging' and 'pprint' but finds other modules like boto3, os, sys, docopt and typing

My configuration is setup such that my interpreter is the system version of Python 3.7 located at /usr/local/bin/python3.7

I have tried setting 'Code compatibility inspection' to python 3.7 only

I have tried turning off 'Code compatibility inspection'

The code runs fine but PyCharm indicates multiple errors

0

@Dspells

Have you updated to the latest version 2019.1 ?

Have you tried removing and recreating project interpreter?

0

Removing and recreating the project interpreter seems to have fixed the problem. Thanks.

1

This seems to happen every now and then. Removing and recreating interpreter is a bit tedious, but it works.

1

So what is the procedure to remove and create the interpreter? I've done every permutation I can think of (select no interpreter, clicking apply, select 3.7 interpreter; select no interpreter, clicking OK, close the window, reopen window, select 3.7 interpreter; etc.) but the problem does not go away for me. 

Running 2019.2.3

0

You can just configure a new interpreter. If the issue with python 2.7 syntax errors in python3 code remains, check the first comment https://intellij-support.jetbrains.com/hc/en-us/community/posts/206602395/comments/205707425

0

Apparently, it takes awhile for the changes to take effect. I made several changes as suggested in this thread on a Friday and nothing worked. I then shut down PyCharm for the weekend. I opened PyCharm the following Monday and the issue now longer exists.

0

After a lot of fiddling with the settings and creating new environment as described above I realized that after installing a new package, (dateutil in my case) my Python version downgraded silently from Python 3.7 to 2.7. I'm using PyCharm 2019.2.3 and conda as package manager. The dateutil package itself is not the issue, it can be any package. The frustrating thing was that I didn't get any warnings or message to what actions will be performed upon new package installation and how it will affect the current environment. To prevent this one needs to tick the `Options` box and supply --freeze-installed. This fails to install the package if it doesn't satisfy the dependencies. This option needs to be supplied every time though.

Of course, there is always the option to use the terminal. However, it would be nice to use the IDE and get a confirmation prompt similar to when using the command line. Are there any interpreter or package manager settings that I missed? Any chance this will be added in the future?

1

In CLion 2019.2.4, I had to do the following to eliminate Python2 errors in Python3 code:

  1. File / Settings / Build, Execution, Deployment / Python Interpreter
  2. Select <No Interpreter>
  3. Click gear icon to right of Project Interpreter combo box, select "Show All..."
  4. Select each Python3 interpreter and click the minus button (-) to delete so that there are no Python3 interpreters in the list (I deleted my Python2 interpreters as well since I am no longer using Python2)
  5. Click OK
  6. Click OK
  7. Close CLion
  8. Reopen CLion
  9. File / Settings / Build, Execution, Deployment / Python Interpreter
  10. Click gear icon to right of Project Interpreter combo box, select "Add..."
  11. Click "System Interpreter" on left, select a Python3 "Base interpreter" (you may want a Virtualenv)
  12. Click OK
  13. Select the newly created Python3 interpreter for the "Project Interpreter"
  14. Click OK
2

I still get this error from my PyCharm 2021.1.3.

And the above solution does not work for me.

0

I solved the issue after unchecking 2.7 from Settings> Inspections> Code compatibility inspection.

1

Just wanted to comment here that none of the above worked for me except this particular piece. Ensure that 2.7 is unchecked in the list on the bottom right.

7

I have the same problem. And @Chris solution worked for me. Thank you. 

0

Hey, I have no idea why the inspections got customized. Thanks to @Chris I just reverted the inspection settings to default!

0

Chris' solution worked for me too. Thanks a lot!

0

Please sign in to leave a comment.