【问题标题】:How to setup working directory in VS Code for pylint?如何在 VS Code 中为 pylint 设置工作目录?
【发布时间】:2018-04-15 23:27:48
【问题描述】:

我有一个具有这种结构的项目:

├── .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

【问题讨论】:

    标签: python visual-studio-code vscode-settings pylint


    【解决方案1】:

    您可以通过在项目根目录中创建一个.env 文件来解决这个问题:

    PYTHONPATH=./src
    

    【讨论】:

    • 这显然行不通除非export PYTHONPATH=./src在终端。
    • @gented 即使您在工作区设置中设置了 pyenv 路径也不行?
    • 使用当前的 VS Code Python 扩展,如果您的工作区文件夹中有包含上述内容的 .env 文件,则不需要运行 export PYTHONPATH=./src。该扩展将自动使用在.env 文件中设置的任何环境变量。 See the docs for more info.
    • 这适用于我,但不适用于我在 VScode 中运行文件时。有什么提示吗?
    【解决方案2】:

    将此行添加到您的settings.json 文件中(在.vscode 目录中)。

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

    【讨论】:

    • 如果您想通过 GUI 找到它 => 转到 Settings => Extensions => Python => "Auto Complete: Extra Paths" => 在 settings.json 中编辑(希望将来会这样更改为输入字段的 GUI 动态列表 :) )
    【解决方案3】:

    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

    【讨论】:

    • 不,PYTHONPATH 是 Python 查找模块的目录列表。 See the docs.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多