首页 > 解决方案 > 简单的问题:在python3 env中,为什么不能运行简单的打印?

问题描述

print("hello", end="")

输出:

[Running] python -u "/home/a/code/projects/project1.py"
  File "/home/a/code/projects/project1.py", line 1
    print("hello", end="")
                      ^
SyntaxError: invalid syntax

设置.json

{
    "python.linting.pylintEnabled": false,
    "python.linting.enabled": true,
    "python.linting.flake8Enabled": true,
    "python.linting.pydocstyleEnabled": false,
    "python.pythonPath": "/usr/bin/python3.6"
}

一直在查找为什么代码在 vscode 中的旧版 python 上运行...无法弄清楚。

标签: pythonpython-3.xpython-2.7

解决方案


那是因为您使用的是 Python 2:

Python 2.7.16 (default, Oct 10 2019, 22:02:15) 
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello", end="")
  File "<stdin>", line 1
    print("hello", end="")
                      ^
SyntaxError: invalid syntax

在 Python3 中它不会发生:

Python 3.7.3 (default, Dec 20 2019, 18:57:59) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello", end="")
hello>>> 

推荐阅读