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 文件,成功了!