pylint false positive E0401 import errors in vscode while using venv

前端 未结 4 1189
醉话见心
醉话见心 2021-02-05 06:40

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

I ran a pip install pylint

相关标签:
4条回答
  • 2021-02-05 07:25

    Just my $0.02 on how I fixed it in my situation.

    My problem was totally related to having pylint installed globally, and coding in a venv. vscode was trying to use the globally installed pylint which simply was not aware of dependencies I installed in my Python venv. This answer solved my problem. It points here which explained how to configure vscode to run using the venv for my project. Once i did that vscode immediately threw a warning saying I had no linting tool installed and prompted me to install one. Once that was done my linting false-positives went away.

    0 讨论(0)
  • 2021-02-05 07:26

    Expanding on Gatmando's answer, you need to tell vscode to use the pylint in your .env instead of the global pylint:

    In your workspace settings file: .vscode/settings.json, add python.linting.pylintPath and point it to pylint package in your virtualenv:

    {
        "python.pythonPath": ".env/bin/python",
        "python.linting.pylintPath": ".env/bin/pylint"
    }
    
    0 讨论(0)
  • 2021-02-05 07:40

    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. 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>')"
      ]
      

      or, maybe better

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

      pylint --generate-rcfile > .pylintrc 
      

      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>")'
      

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

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

      from database.database_dispatcher import ...
      

      See this answer by Anthony Sottile.

    0 讨论(0)
  • 2021-02-05 07:42

    To me, pylint is correct in flagging this error here

    the top level module is database (it contains an __init__.py file)

    Your import should look like (fully absolute)

    from database.database_dispatcher import ...
    

    or (explicit relative) (yes! the . before the module name is intentional)

    from .database_dispatcher import ...
    

    My follow-up guess is that you're currently invoking your script as python ./database/main.py ... which is putting ./database at the beginning of sys.path so it would appear that your imports are working correctly -- this is side-stepping your module structure however. You should be invoking your script using python -m database.main ... instead.

    Note that implicit relative imports were removed in python 3.x -- though this (imo) wart of script sys.path insertion remains.

    0 讨论(0)
提交回复
热议问题