python - 在 vscode 中调试时如何覆盖/更正 PYTHONPATH?
问题描述
我有一个这种结构的项目:
.
├── hacktcha
│ ├── cli.py
│ ├── conf.py
│ ├── generator
│ │ ├── base.py
│ │ └── __init__.py
│ ├── hacktcha.py
│ ├── __init__.py
│ └── server.py
├── poetry.lock
├── pyproject.toml
└── tests
├── __init__.py
└── test_hacktcha.p
当我尝试通过以下 launch.json 配置调试 cli.py 时:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: CurrentFile",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
我收到了这个错误:
Exception has occurred: ModuleNotFoundError
No module named 'hacktcha.hacktcha'; 'hacktcha' is not a package
During handling of the above exception, another exception occurred:
File "/apps/hacktcha/hacktcha/cli.py", line 4, in <module>
from hacktcha.hacktcha import HacktchaLearner
在 cli.py 开头添加一些调试代码:
print(sys.path)
sys.path.pop(0)
sys.path.insert(0, "/apps/hacktcha")
我得到 PYTHONPATH 为:
['/apps/hacktcha/hacktcha', '/conda/envs/hacktcha/lib/python37.zip', '/conda/envs/hacktcha/lib/python3.7', '/conda/envs/hacktcha/lib/python3.7/lib-dynload', '/conda/envs/hacktcha/lib/python3.7/site-packages']
该应用程序可以继续。
我的问题是,当 vscode 将错误的路径(“/apps/hacktcha/hacktcha”)注入 PYTHONPATH 时?如何纠正这种行为?
我尝试在 launch.json 中添加 PYTHONPATH 为:
"env": ["PYTHONPATH":"blahblah"]
这不起作用,因为错误的路径('/app/hacktcha/hacktcha')需要更高的优先级。
我想知道是否有人遇到过同样的事情并且知道为什么以及如何解决?
解决方案
推荐阅读
- r - 一致性矩阵
- python - KeyError:discord.py 中的“594750729810477063”
- javascript - 我怎样才能 document.getElementbyID 的变量?
- java - 如何使用 Lambda 表达式将 ArrayList 中的一组值添加到另一个 ArrayList
- visual-c++ - 如何标记这个字符串
- python - 删除不是 a 到 z 的所有内容
- python - 在行首添加带有特定字符串的字符 - Python
- angular - 无法在注销后的 url 上重定向 azure AD
- nlog - NLog 截断语法
- javascript - 如何检索 Firebase-Instance-ID-token?