美文网首页
VSCode 使用 code-runner 运行 Python

VSCode 使用 code-runner 运行 Python

作者: 星塵子 | 来源:发表于2020-03-29 14:15 被阅读0次

    code-runner 运行在输出窗口时报错: Python was not found

    1.png
    有两种解决办法,都是修改配置 : settings.json
    1. 将 code-runner输出移至vscode 的内置终端输出:
    "code-runner.runInTerminal": true,
    

    配置保存后重新运行:


    2.png
    1. 指定 python 路径: 先定义 python路径,在配置code-runner 内引用 python 路径
    "python.pythonPath": "D:\\Soft\\Anaconda\\python.exe",
    "code-runner.executorMap": {
        "python": "$pythonPath -u $fullFileName",
    },
    

    配置保存后运行:


    3.png

    可以运行了,但产生了新问题:中文乱码。

    输出窗口中文乱码

    也有两种解决办法:

    1. 见上面一个问题的第一种解决办法: code-runner输出移动到vscode 内置终端输出 前提是中文在终端内显示正常。
    2. 设置变量 PYTHONIOENCODING=utf8,以下方法二选一:
      2.1 添加系统的环境变量
      2.2 运行时先设置变量
      "code-runner.executorMap": {
      "python": "set PYTHONIOENCODING=utf8 & $pythonPath -u $fullFileName",
      },
      

      我使用了第二种方法,保存后运行:


      4.png

    使用 code-runner 运行时会运行文件注释头的命令 /usr/bin/env python,而非配置的 python

    pyhont文件配置有文件头时,会产生以下错误:

    5.png 6.png

    修改配置:settings.json

    "code-runner.respectShebang": false
    

    保存后运行:


    7.png

    切换 conda 环境:

    1. 右下角选择
      8.png
    2. Ctrl + Shift + P 选择 Pyhont: Interpreter
      9.png

    相关文章

      网友评论

          本文标题:VSCode 使用 code-runner 运行 Python

          本文链接:https://www.haomeiwen.com/subject/cvnyuhtx.html