简体   繁体   中英

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. For linting I use 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:

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. confirmed both in the terminal of vscode and in the bottom of the 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:

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:

{
   
    "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"}
    }
]

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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