VSCode 使用 code-runner 运行 Python
2020-03-29 本文已影响0人
星塵子
当 code-runner
运行在输出窗口时报错: Python was not found

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

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

可以运行了,但产生了新问题:中文乱码。
输出窗口中文乱码
也有两种解决办法:
- 见上面一个问题的第一种解决办法: 将
code-runner
输出移动到vscode
内置终端输出 前提是中文在终端内显示正常。 - 设置变量
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
文件配置有文件头时,会产生以下错误:


修改配置:settings.json
"code-runner.respectShebang": false
保存后运行:

切换 conda
环境:
- 右下角选择
8.png
-
Ctrl + Shift + P
选择Pyhont: Interpreter
9.png