我正在尝试在Python中从上一级目录进行导入。
import sys
sys.path.append('..')
from cn_modules import exception
当我尝试运行构建任务时,从VSCode中获取了以下错误:
ImportError: No module named cn_modules
在终端(python)中没有任何错误,同样的代码可以正常工作。
当我尝试从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}"
}
]
}
任务清单.json
{
"version": "0.1.0",
"command": "/usr/bin/python",
"isShellCommand": true,
"args": ["${file}"],
"showOutput": "always",
"env": {},
"envFile": "${workspaceRoot}/.env",
"pythonPath": "${config:python.pythonPath}"
}