1. 背景
我非要用vscode,不用pycharm,哼!
2. 问题
由于 import xx
自定义的模块, python run 的时候会报错 No module named ‘xxx‘
报错信息:
Traceback (most recent call last):
File "d:\work\sf_financial_data_auto_test\db\gen.py", line 2, in <module>
from settings import mixin_db, ROOT_DIR
ModuleNotFoundError: No module named 'settings'
3. 解决办法
3. 1. 在.vscode下的launch.json中添加 "env": {"PYTHONPATH": "${workspaceRoot}"}:
{
"version": "0.2.0",
"configurations": [
{
// 省略其他
"env": {"PYTHONPATH": "${workspaceRoot}"}
}
]
}
3.2. 在.vscode下的settings.json
中添加:
{
// 省略其他
"terminal.integrated.env.windows": {
"PYTHONPATH": "${workspaceFolder};${env:PYTHONPATH}"
}
}
重启一下vscode,再次运行,就可以成功啦!
再次验证
或者直接 打开 settings.json
"launch": {
"configurations": [
{
// 省略其他
"env": {"PYTHONPATH": "${workspaceRoot}"}
}
],
"compounds": []
},
"terminal.integrated.env.windows": {
"PYTHONPATH": "${workspaceFolder};${env:PYTHONPATH}"
}