vs code下運行python 腳本報錯:
FileNotFoundError: [Errno 2] No such file or directory: './xxx.py'
原因:
vs code 運行python文件,默認打開當前工作區的根路徑,運行該工作區子文件夾中py文件,會含有相對路徑,則報錯
網上解決方案:
配置launch.json 文件添加 "cwd": "${fileDirname}"
or
設置 --> terminal --> python --> 勾選 Execute In File Dir
親測無效!
Mac 下解決方案:
下載插件【Terminal Here】
設置(快捷鍵 Command+,)
搜索:Terminal Here: Create Terminal
終端執行命令: python xxx.py
小結
暫未找到Mac下vs code 配置相對路徑的解決方案,如有,請指教!
推薦閱讀:VScode 官方文檔