pylint false positive E0401 import errors in vscode while using venv

前端 未结 4 1193
醉话见心
醉话见心 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: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('')"
      ]
      

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

      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.

提交回复
热议问题