pylint 误报 E0401 在使用 venv 时在 vscode 中导入错误 [英] pylint false positive E0401 import errors in vscode while using venv

查看:33
本文介绍了pylint 误报 E0401 在使用 venv 时在 vscode 中导入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个文件夹中的 mac os 上使用 python3.6 创建了一个 venv/Users/kim/Documents/Apps/PythonApps/python36-miros-a3

I created a venv using python3.6 on my mac os in this folder /Users/kim/Documents/Apps/PythonApps/python36-miros-a3

我在激活虚拟环境后运行了 pip install pylint

I ran a pip install pylint after I activated the virtual env

我的工作区在 /Users/kim/Documents/Apps/WebApps/miros-a3

在我的 vscode 工作区中,我有以下工作区设置

Inside my vscode workspace, I have the following Workspace settings

{
    "folders": [
        {
            "path": "."
        }
    ],
    "settings": {
        "python.pythonPath": "/Users/kim/Documents/Apps/PythonApps/python36-miros-a3/bin/python3.6",
        "python.venvPath": "/Users/kim/Documents/Apps/PythonApps"
    }
}

我尝试为 pylint 设置自定义路径并更改 venvpath.

I have tried setting a custom path for the pylint and also changing the venvpath.

pylint 一直抱怨 import 语句说它不存在.

The pylint kept complaining about the import statement saying it does not exist.

如你所见,它们在同一个文件夹中,我绝对可以执行我的python文件.

As you can see, they are in the same folder, and I can definitely execute my python files.

我该怎么做才能避免此类误报导入错误?

What can I do to avoid these kind of false positive import errors?

我还尝试了以下方法:

  1. 转到命令行打开虚拟环境,然后输入 code 以按照此处的推荐激活 vscode https://code.visualstudio.com/docs/setup/mac
  2. 也试过这个https://donjayamanne.github.io/pythonVSCodeDocs/docs/troubleshooting_linting/
  1. go to commandline turn on the virtual env and then type code to activate the vscode as recommended here https://code.visualstudio.com/docs/setup/mac
  2. also tried this https://donjayamanne.github.io/pythonVSCodeDocs/docs/troubleshooting_linting/

推荐答案

Pylint 有一些怪癖.在这种情况下,它不知道在哪里可以找到您的模块,因为它位于您的 venv 路径的子目录中.解决这个问题:

Pylint has some quirks. In this case it doesn't know where to find your module because it's in subdirectory of your venv path. To solve this:

  1. 将此设置放在您的工作区或文件夹设置中:

  1. Put this setting in your workspace or folder settings:

"python.linting.pylintArgs": [
    "--init-hook",
    "import sys; sys.path.append('<path to folder your module is in>')"
]

或者,也许更好

生成 .pylintrc 文件.从带有 venv 激活运行的集成终端:

Generate .pylintrc file. From integrated terminal with venv activated run:

pylint --generate-rcfile > .pylintrc 

然后打开生成的文件并取消注释 init-hook= 部分:

then open the generated file and uncomment the init-hook= part to be:

init-hook='import sys; sys.path.append("<path to folder you module is in>")'

阅读 .pylintrc 并根据需要调整设置.在这两种情况下,路径都应指向您的数据库"文件夹.

Read the .pylintrc and tweak settings if you wish. In both cases path should point to your 'database' folder.

了解 pylint 设置后,请以正确的方式进行:

After learning about pylint settings, do it the right way:

from database.database_dispatcher import ...

这个答案 作者:安东尼·索蒂尔.

See this answer by Anthony Sottile.

这篇关于pylint 误报 E0401 在使用 venv 时在 vscode 中导入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆