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: https://go.microsoft.com/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: ModuleNotFoundError
No module named 'xxx'
解决:
- 代码中查看当前路径、加入绝对路径
当前路径(’./’)就是你调试的那个代码所在的目录,所以如果找不到xxx,就要把xxx模块所在的目录加入到 sys.path 中去。(注意你在终端运行的时候,当前路径和在调试的时候当前路径是不一样的)
import os
import sys
sys.path.append(os.path.abspath('./'))
print(sys.path)
|