简体   繁体   English

VsCode + Python + Linter + 诗歌:麻烦

[英]VsCode + Python + Linter + Poetry: Troubles

I solved once this problem but I do not remember how.我曾经解决过这个问题,但我不记得如何了。 I have both to use VSCode with a Linting and, of course, run the debugger.我必须同时使用带有 Linting 的 VSCode,当然,还要运行调试器。 For linting I use Pylama.对于棉绒,我使用 Pylama。 The folder structure is as follows.文件夹结构如下。

├── poetry.lock
├── pyproject.toml
├── README.md
└── src
    ├── app
    │   ├── api
    │   ├── core
    │   │   ├── config.py
    │   │   ├── __pycache__
    │   │   │   
    │   │   └── worker.py
    │   ├── crud
    │   ├── __init__.py
    │   ├── lib
    │   │   ├── be.py
    │   │   ├── __init__.py
    │   │   ├── logger.py
    │   │   ├── omoi.py
    │   │   └── __pycache__
    │   │        
    │   ├── main.py

The main.py is very simple and the Linter doesn't complain anything: main.py非常简单,Linter 没有任何抱怨:

import argparse
import lib.logger
from core.config import settings
from core.worker import Worker


if __name__ == "main":
    parser = argparse.ArgumentParser(description='Basic Worker')
    parser.add_argument('--mode', type=str,
                        choices=['test', 'dummy', 'prod'],
                        default='prod')
    args = parser.parse_args()
    mode = args.mode
    main_worker = Worker()
    main_worker.start()

As interpreter, I choose one created by poetry shell command.作为解释器,我选择了一个由诗歌 shell 命令创建的。 confirmed both in the terminal of vscode and in the bottom of the IDE:在 vscode 的终端和 IDE 的底部都确认了:

service on main [✘!?] is 📦 v0.1.0 via 🐍 v3.10.6 (service-v4nvssyk-py3.10)

Running the program from the terminal:从终端运行程序:

service/src# pyton main.py

works great.效果很好。

But in debug mode of VSCode (F5), I got always an Exception:但是在 VSCode (F5) 的调试模式下,我总是遇到异常:

ModuleNotFoundError
No module named 'app'
  File "/home/me/service/src/app/lib/logger.py", line 4, in <module>
    from app.core.config import settings
  File "/home/me/service/src/app/main.py", line 2, in <module>
    import lib.logger

Why?为什么? The launch.json is:该launch.json为:

{
   
    "configurations": [
        
        {
            "name": "Python: current file",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,
            "env": { "PYTHONPATH": "${workspaceRoot}"}
        }
    ]
}

I found a solution.我找到了解决方案。 The debug configuration shall be: { "configurations": [调试配置应为:{“配置”:[

    {
        "name": "Python: file corrente",
        "type": "python",
        
        "request": "launch",
        "program": "${file}",
        "console": "integratedTerminal",
        "justMyCode": true,
        "cwd": "${workspaceRoot}/src",
        "env": { "PYTHONPATH": "${workspaceRoot}/src"}
    }
]

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM