失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > mac m1 python vscode 远程调试代码(debug)配置 报错:Exception has occurred: ModuleNotFoundError

mac m1 python vscode 远程调试代码(debug)配置 报错:Exception has occurred: ModuleNotFoundError

时间:2019-02-01 08:42:10

相关推荐

mac m1 python vscode 远程调试代码(debug)配置 报错:Exception has occurred: ModuleNotFoundError

1. 配置python解释器

修改程序调试时的python解释器(interpreter)

有几个地方需要设置(不确定哪个地方起作用了还是说都要配置,vs这玩意我觉得设置起来没有Pycharm友好):

1)Code -> preferences -> settings,在弹出的页面上方搜索 interp 然后点击 Remote [SSH: xx.xxx.xxx.xxx] ,点击左侧 Extensions -> Python,把你需要的解释器路径粘贴在框中,如/home/user1/miniconda3/envs/xxx_yolov5/bin/python

搜索 python.pythonPath 进行同样配置

2)在vscode左侧文件列表,在.vscode文件夹下修改 launch.json,内容如下:

{// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: /fwlink/?linkid=830387"version": "0.2.0","configurations": [{"name": "Python: Current File","type": "python","request": "launch","program": "${file}",// "console": "integratedTerminal","stopOnEntry": false,"cwd": "${workspaceRoot}","pythonPath": "${config:python.pythonPath}","env": {"PYTHONPATH":"${workspaceRoot}"},"envFile": "${workspaceRoot}/.env","debugOptions": ["WaitOnAbnormalExit","WaitOnNormalExit","RedirectOutput"]}]}

3)在vscode左侧文件列表,在.vscode文件夹下新建 settings.json,内容如下:

{"python.pythonPath": "/home/user1/miniconda3/envs/xxx_yolov5/bin/python","code-runner.runInTerminal": false, "code-runner.executorMap": {"python": "python3 -u",}}

2. 解决报错:Exception has occurred: ModuleNotFoundError

明明在命令行运行得好好的,TM就是找不到

Exception has occurred: ModuleNotFoundErrorNo module named 'xxx'

解决:

代码中查看当前路径、加入绝对路径

当前路径(‘./’)就是你调试的那个代码所在的目录,所以如果找不到xxx,就要把xxx模块所在的目录加入到 sys.path 中去。(注意你在终端运行的时候,当前路径和在调试的时候当前路径是不一样的

import osimport syssys.path.append(os.path.abspath('./'))print(sys.path)

3. 配置调试设置

launch.json

{// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: /fwlink/?linkid=830387"version": "0.2.0","configurations": [{"name": "Python: Current File","type": "python","request": "launch","program": "${file}","console": "integratedTerminal","justMyCode": true,"args":["--img", "1280","--batch","1","--epochs","30","--data","xxx","--weights","xxx","--cfg","xxx","--hyp","xxx","--name","xxx",]},]}

如果觉得《mac m1 python vscode 远程调试代码(debug)配置 报错:Exception has occurred: ModuleNotFoundError》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。