首页 > 解决方案 > 在 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')需要更高的优先级。

我想知道是否有人遇到过同样的事情并且知道为什么以及如何解决?

标签: pythonvisual-studio-code

解决方案


推荐阅读