在 Visual Studio 代码中从正确的 anaconda 环境启动 PyLint

Start PyLint from correct anaconda environment in Visual Studio Code

我正在尝试让 PyLint 在 vscode 中自动使用正确的 conda 环境,但仍然出现导入错误:[pylint] E0401:Unable to import 'django',虽然:

您必须在此 conda 环境中安装 pylint

  1. 使用 activate env_name (Windows) 或 source activate env_name.

    激活给定环境
  2. 在此环境中安装pylint

    conda install pylint # or 'pip install pylint'
    
  3. 最后,重启vscode。

来源:https://github.com/DonJayamanne/pythonVSCode/wiki/Troubleshooting-Linting

即使在正确安装 python 和 pylint 后,由于 visual studio 代码中的 pylintArgs 配置错误,pylint 仍面临问题。

通过以下用户设置问题得到解决

"python.linting.pylintArgs": [
        "--load-plugins", 
        "pylint_django"
    ] 

您只需要确保您在使用 conda 本身创建的相同虚拟环境中工作。基本上你需要启用“Python: Enable Linting command”。通常在左下角,VS Code 会告诉你你在什么环境中工作。如果您通过 anaconda-navigator GUI 打开 VS Code(即使您使用正确的相应环境打开它)。 VS Code,默认情况下使用(基本)conda 环境打开编辑器。这可能就是混乱。具有讽刺意味的是,Linting 正在做它打算做的事情。通过左下角的提示,只需将环境更改为您为项目定制的环境。关闭编辑器,并完全关闭 anaconda-navigator GUI。只需使用终端更新和升级您的系统(如果您使用的是基于 Linux 的 OS)。快速更新 conda,以确保没有任何问题。重启 anaconda-navigator,打开 VS Code。如果你 运行 编辑器的内部终端,你会看到它通过 (base) 环境打开,它会自动切换到正确的相应 conda 环境,它自己这样做;

source /home/user/anaconda3/bin/activate
(base) user@machine:~$ source /home/user/anaconda3/bin/activate
(base) user@machine:~$ conda activate your_env
(your_env) user@machine:~$  

PS - 我个人发现使用 Anaconda Navigator 让我的生活更轻松。

也许为了进一步阅读,只需快速浏览 VS 代码文档 https://code.visualstudio.com/docs/python/linting。黑客攻击快乐!