簡體   English   中英

如何在 VS Code 中為 pylint 設置工作目錄?

[英]How to setup working directory in VS Code for pylint?

我有一個具有這種結構的項目:

├── .git
├── .gitignore
├── README.md
├── requirements.txt
└── src

Pylint 默認從項目根目錄運行,我的所有導入都有錯誤,因為src目錄中的源根目錄。 我嘗試在 settings.json 中設置 linter 路徑,但是 linter 不起作用

"python.linting.pylintPath": "cd src && pylint"

問題是:如何在 VS Code 中更改 pylint 的源根目錄? 我使用這個擴展https://github.com/DonJayamanne/pythonVSCode

您可以通過在項目根目錄中創建一個包含以下內容的.env文件來解決此問題:

PYTHONPATH=./src

在您的settings.json文件中添加這一行(在.vscode目錄中)。

"python.autoComplete.extraPaths": ["./src"],

PYTHONPATHpython的路徑,而不是工作目錄。

更好的方法是自定義Settings.jsonlaunch.json ,這樣做:

// vi .vscode/Settings.json
{
    "python.pythonPath": "venv/bin/python",
    "python.linting.pylintEnabled": true,
    "python.linting.enabled": true,
    "python.formatting.provider": "autopep8"
}

// vi .vscode/launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: your project name",
            "type": "python",
            "request": "launch",
            "cwd": "${workspaceRoot}/src",
        }
    ]
}

參考: https ://code.visualstudio.com/docs/editor/debugging#_launch-versus-attach-configurations

在 vs-code 中:文件 - 首選項 - 設置 搜索:Pylint "Pylint: Path" - 添加項目

它對我有用!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM