首页 文章

python模块的vscode导入错误

提问于
浏览
4

我试图从一个目录级别进行python导入 .

import sys

sys.path.append('..')
from cn_modules import exception

当我尝试运行构建任务时,我从VSCode收到错误:

ImportError:没有名为cn_modules的模块

The same code works without any error from terminal (python) .
当我尝试运行它时,我遇到了问题 from VSCode Run Build task .
这里有什么问题的任何线索?

安静了一段时间,但无法解决这个问题,任何帮助表示赞赏 .


注意:当我使用vscode进行调试时,这也适用 . 下面是我对launch.json和tasks.json的配置

launch.json

{
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Python Console App",
                "type": "python",
                "request": "launch",
                "stopOnEntry": true,
                "program": "${file}",
                "externalConsole": true,
                "debugOptions": [
                    "WaitOnAbnormalExit",
                    "WaitOnNormalExit"
                ],
                "env": {},
                "envFile": "${workspaceRoot}/.env",
                "console":"integratedTerminal",
                "pythonPath": "${config:python.pythonPath}"
            }
        ]
    }

tasks.json

{
        "version": "0.1.0",
        "command": "/usr/bin/python",
        "isShellCommand": true,
        "args": ["${file}"],
        "showOutput": "always",
        "env": {},
        "envFile": "${workspaceRoot}/.env",
        "pythonPath": "${config:python.pythonPath}"
 }

2 回答

  • -1

    我试着在我的 launch.json 中添加它,然后它就可以了!

    "env": {"PYTHONPATH": "${workspaceRoot}"}
    

    下面是我的 launch.json

    "name": "Python: Current File (Integrated Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "cwd": "${workspaceRoot}",
            "env": {"PYTHONPATH": "${workspaceRoot}"},
            "console": "integratedTerminal"
    
  • 0

    在launch.json文件中,可以尝试设置env:{},作为“env”:

    {"PYTHONPATH":"${workspaceRoot}"}

相关问题