简体   繁体   English

Python2.7 - 安装Python3后解释器语法错误

[英]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).当我尝试在 python27 解释器上写任何东西时,无论我输入什么(不仅仅是print ,实际上任何东西),我都会收到语法错误。 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.我最近安装了 Python3 和 PyCharm,但我仍然想将 python27 用于我拥有的一些遗留代码。 The problem started to happen after I installed Python3 and PyCharm (Not sure if PyCharm may be related or not).在我安装 Python3 和 PyCharm 后问题开始发生(不确定 PyCharm 是否相关)。

my OS is Windows10 x64.我的操作系统是 Windows10 x64。 Any ideas anyone?任何人的想法?

Python27:蟒蛇27: 蟒蛇27

Python3:蟒蛇3:

蟒蛇3

I can reproduce this perfectly by using a non-break space unicode character as the last character:我可以通过使用不间断空格 unicode 字符作为最后一个字符来完美地重现这一点:

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:当我将其复制/粘贴到基于 python 3 的 IPython 中时,它会显示:

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.我希望任何类似空格的 unicode 字符都会发生同样的情况。

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 .在 Python 中输入代码时,使用End转到行尾,然后使用一个或多个Backspace删除任何多余的空格,然后再按Enter If your cursor moves when you press End , that would confirm the issue.如果您在按下End时光标移动,那将确认问题。

If not, then maybe it is whatever the terminal program sends to Python when you press Enter .如果没有,那么当您按下Enter时,它可能是终端程序发送给 Python 的任何内容。

I would advise to just remove python 2.7 and port any remaining 2.7 specific code to python 3.我建议只删除 python 2.7 并将任何剩余的 2.7 特定代码移植到 python 3。

Edit Since you still need Python 2, I would suggest to try running a python 2 script.编辑由于您仍然需要 Python 2,我建议尝试运行 python 2 脚本。 If that works, the problem is probably with the terminal setup (like text encoding, line feed etc.) being different than what Python 2 expects.如果可行,问题可能在于终端设置(如文本编码、换行等)与 Python 2 期望的不同。 If scripts work, try one of the many different terminal programs available for ms-windows.如果脚本有效,请尝试 ms-windows 可用的许多不同终端程序之一。

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

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