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
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
Please sign in to leave a comment.
Have you looked at Settings -> Inspections -> Python -> 'Code compatibility inspection'?
Thank you
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"
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.
Thanks Sergey,
Version 2018.3.1 seems to be working much better.
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
I am having these problems again. Including:
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
@Dspells
Have you updated to the latest version 2019.1 ?
Have you tried removing and recreating project interpreter?
Removing and recreating the project interpreter seems to have fixed the problem. Thanks.
This seems to happen every now and then. Removing and recreating interpreter is a bit tedious, but it works.
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
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
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.
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?
In CLion 2019.2.4, I had to do the following to eliminate Python2 errors in Python3 code:
I still get this error from my PyCharm 2021.1.3.
And the above solution does not work for me.
I solved the issue after unchecking 2.7 from Settings> Inspections> Code compatibility inspection.
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.
I have the same problem. And @Chris solution worked for me. Thank you.
Hey, I have no idea why the inspections got customized. Thanks to @Chris I just reverted the inspection settings to default!
Chris' solution worked for me too. Thanks a lot!