Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pylint false positive E0401 import errors in vscode while using venv

Tags:

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 after I activated the virtual env

My workspace is in /Users/kim/Documents/Apps/WebApps/miros-a3

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"     } } 

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

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

enter image description here

enter image description here

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?

I have also tried the following:

  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/
like image 536
Kim Stacks Avatar asked Jun 29 '18 06:06

Kim Stacks


People also ask

How do I fix Pylint import error in Vscode?

db' pylint(import-error) showing up. This is because VS Code is not running the Virtual Environment of the app. To fix it, run cmd-shift-p (or click View -> Command Palette and run the command Python: Select Interpreter. VS Code will show you a list of Python interpreters found.


1 Answers

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.

like image 91
Braca Avatar answered Sep 19 '22 22:09

Braca