简体   繁体   中英

Python2.7 - Interpreter syntax error after installing Python3

When I'm trying to write anything on the python27 interpreter, I'm getting syntax error, no matter what I'm typing (Not just print , literally anything). Couldn't find anyone else experiencing the same issue, so no clue why this is happening.

I've recently installed Python3 and PyCharm, but I still want to use python27 for some legacy code I have. The problem started to happen after I installed Python3 and PyCharm (Not sure if PyCharm may be related or not).

my OS is Windows10 x64. Any ideas anyone?

Python27: 蟒蛇27

Python3:

蟒蛇3

I can reproduce this perfectly by using a non-break space unicode character as the last character:

Python 2.7.16 (default, Apr 11 2019, 01:11:37) 
[GCC 4.2.1 Compatible FreeBSD Clang 6.0.0 (tags/RELEASE_600/final 326565)] on freebsd11
Type "help", "copyright", "credits" or "license" for more information.
>>> print "Test" 
  File "<stdin>", line 1
    print "Test" 
                ^
SyntaxError: invalid syntax
>>> import os 
  File "<stdin>", line 1
    import os 
             ^
SyntaxError: invalid syntax
>>> 111 
  File "<stdin>", line 1
    111 
       ^
SyntaxError: invalid syntax
>>>

When I copy/paste that into IPython based on python 3, it shows up:

In [1]: test = 'print "Test" '                                                                           
Out[1]: 'print "Test"\xa0'

In [2]: import unicodedata

In [3]: unicodedata.name(test[-1])                                                                           
Out[3]: 'NO-BREAK SPACE'

I expect the same to happen with any space-like unicode character.

When typing code into Python, use End to go to the end of the line, and then one or more Backspace to remove any extra whitespace, before you press Enter . If your cursor moves when you press End , that would confirm the issue.

If not, then maybe it is whatever the terminal program sends to Python when you press Enter .

I would advise to just remove python 2.7 and port any remaining 2.7 specific code to python 3.

Edit Since you still need Python 2, I would suggest to try running a python 2 script. If that works, the problem is probably with the terminal setup (like text encoding, line feed etc.) being different than what Python 2 expects. If scripts work, try one of the many different terminal programs available for ms-windows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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