无法在VS Code的输出窗格中使用正确的python解释器

时间:2019-03-27 10:08:07

标签: python json visual-studio-code

1)我在Mac osx上安装了Python3.7.3,并在终端上进行了确认(使用“ python3 --version”)。

2)然后我在vs代码中选择了3.7.3解释器。我可以看到该解释器是底部状态栏中使用的解释器。

3)调试控制台显示我在运行以下代码段时正在运行Python3.7.3,方法是在VS-Code中按F5键以在调试器中启动代码。我也可以直接从终端运行它(python3 app.py)。

import sys
print(sys.version)
variable = "awesome"
print(f"Today is {variable}")

问题:

4)当我尝试打印(sys.version)时,“输出”窗格仍显示我正在运行Python2.7.10。

当我尝试使用f字符串时,在输出窗格中看到语法错误...终端和调试控制台都能够按预期打印该字符串。

如何使我的输出窗格解释python3.7而不是2.7?我想在settings.json文件中需要做些什么,但是我不确定从哪里开始。

这是我所有的launch.json文件。我认为这是相关的吗?

    "version": "0.2.0",
    "configurations": [


        {
            "name": "Python: Current File (Integrated Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        },
        {
            "name": "Python: Remote Attach",
            "type": "python",
            "request": "attach",
            "port": 5678,
            "host": "localhost",
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "."
                }
            ]
        },
        {
            "name": "Python: Module",
            "type": "python",
            "request": "launch",
            "module": "enter-your-module-name-here",
            "console": "integratedTerminal"
        },
        {
            "name": "Python: Django",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "console": "integratedTerminal",
            "args": [
                "runserver",
                "--noreload",
                "--nothreading"
            ],
            "django": true
        },
        {
            "name": "Python: Flask",
            "type": "python",
            "request": "launch",
            "module": "flask",
            "env": {
                "FLASK_APP": "app.py"
            },
            "args": [
                "run",
                "--no-debugger",
                "--no-reload"
            ],
            "jinja": true
        },
        {
            "name": "Python: Current File (External Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "externalTerminal"
        }
    ]
}

这是我的全局settings.json:

{
    "python.pythonPath": "/Library/Frameworks/Python.framework/Versions/3.7/bin/python3",
    "liveServer.settings.donotShowInfoMsg": true,
    "files.autoSave": "afterDelay",
    "code-runner.clearPreviousOutput": true,
}

这是我的workspace.json:

{
    "folders": [
        {
            "path": "/Users/sethgory/Desktop/school/CPSC50100/Python/Week01"
        }
    ],
    "settings": {}
}

这是我尝试使用f字符串时的输出:

[Running] python -u "/Users/sethgory/Desktop/school/CPSC50100/Python/Week01/Hello.py"
  File "/Users/sethgory/Desktop/school/CPSC50100/Python/Week01/Hello.py", line 5
    print(f"Today is {variable}")
                               ^
SyntaxError: invalid syntax

[Done] exited with code=1 in 0.909 seconds

当我运行print(sys.version)时输出:

[Running] python -u "/Users/sethgory/Desktop/school/CPSC50100/Python/Week01/Hello.py"
2.7.10 (default, Oct 23 2015, 19:19:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)]

[Done] exited with code=0 in 2.421 seconds

当我按F5键进行调试时,这就是调试控制台中出现的内容(再次运行print(sys.version)):

3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21) 
:undefined
[Clang 6.0 (clang-600.0.57)]

这是我在调试(再次运行print(sys.version))时在终端中得到的内容:

Week01 sethgory$ cd /Users/sethgory/Desktop/school/CPSC50100/Python/Week01 ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 /Users/sethgory/.vscode/extensions/ms-python.python-2019.3.6139/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 55179 /Users/sethgory/Desktop/school/CPSC50100/Python/Week01/Hello.py 
3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21) 
[Clang 6.0 (clang-600.0.57)]

最后,当我使用“ python3 Hello.py”从终端运行程序时,这就是我到达的位置(再次运行print(sys.version)):

3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21) 
[Clang 6.0 (clang-600.0.57)]

到目前为止我已经尝试过的一些事情:

我尝试在全局和工作空间设置中使用以下三个路径(一次一个!),似乎都没有改变任何行为:

这是绝对的,我认为:

{
        "python.pythonPath": "/Library/Frameworks/Python.framework/Versions/3.7/bin/python3"
    }

这对env文件夹是绝对的:

{
        "python.pythonPath": "/Users/sethgory/Desktop/school/CPSC50100/Python/Week01/env/bin/python3"
    }

这是相对于env文件夹:

{
        "python.pythonPath": "env/bin/python3"
    }

我试图将alias python='python3'添加到我的〜/ .bash_profile中,并在终端中出现一些新行为。现在,我可以使用“ python”来引用python3(而在我需要实际键入“ 3”之前。但是主要问题仍然存在(vs代码中的输出窗口仍使用python2.7.2)

Seth-Gorys-MacBook-Pro:~ sethgory$ python --version
Python 3.7.3
Seth-Gorys-MacBook-Pro:~ sethgory$ python3 --version
Python 3.7.3

我应该提到我确实已经正确安装了Python扩展。正确的解释器3.7.3显示在状态栏中。

1 个答案:

答案 0 :(得分:0)

显然,在运行脚本时,将调用默认的Python(版本2)。您可以通过将以下行添加到〜/ .bash_profile中来为命令添加别名:

$ alias python='python3'

python命令现在将引用python3。

如果要执行原始的Python(指python2),则可以转义别名(因此\python将在不触摸别名的情况下启动python2)。


您可能需要重新启动VSCode。也许在OSX下,但必须重新启动系统(不确定,我使用Linux)。