python代码pycharm 中可以运行 vscode无法运行

问题:pycharm 中可以运行,切到 vscode中时无法运行,都是路径无法读取到导致模块无法加载。
分析:主要原因有可能是VSCode默认使用项目文件夹根目录作为工作目录(cwd),这会使得子文件夹中的程序无法使用相对路径。

1.第一步vscode中设置一下:
文件 => 首选项 => 设置 中搜索Execute in File Dir,勾上,即可正常运行。

工作区和用户中都勾选上

2.第二步修改lauhch.json
当前工程目录下 => 运行 => 打开配置 弹出launch.json文件
默认文件内容:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: 当前文件",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,
            "cwd": "${fileDirname}" //新增内容
        }
    ]
}
posted @ 2023-12-27 15:24  SevenKZhou  阅读(249)  评论(0编辑  收藏  举报