VSCODE 点击右上角运行按钮,报错如下:
[Running] python -u "test.py"
Traceback (most recent call last):
File "test.py", line 3, in <module>
from PIL import Image, ImageEnhance
ImportError: No module named PIL
[Done] exited with code=1 in 0.231 seconds
直接命令行下运行,没有任何报错
python3 test.py
写个test_evn.py 测试python默认执行环境
#!/usr/local/bin/python3.6
import sys
print (sys.path)
结果:
[Running] python -u "/Users/your_name/test.py"
['/Users/your_name', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Python/2.7/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC']
[Done] exited with code=0 in 0.137 seconds
问题找到了,现在解决问题,找到vscode默认的执行配置文件
mac 组合键:command + shift + p
输入:setting
选择红框点击,进入 command+F 搜索 python -u
提前编辑好如下json格式
"code-runner.executorMap": {
"python": "python3 -u",
}
继续 command + shift + p 搜索 setting
然后选择如下红框setting文件
把上边编辑好的json 放入到选择的文件 setting.json尾部:
{
"editor.fontSize": 16,
"fileheader.configObj": {
},
"code-runner.executorMap": {
"python": "python3 -u",
}
}
再次执行之前的test.py 文件,成功了!