Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code - pylinter cannot find module

I started using VS Code for Python development on a Mac and cannot make pylint find a module.

This is my project folder structure:

project_root/
   .env
   .vscode/
       settings.json
   lib/
       # lib containing necessary modules
   sample/
       client/
           EDAMTest.py
   # many more files

I use a virtualenv in which I have installed pylint. The virtual env is activated in the terminal. I started code from within project_root folder via code . in my terminal.

VS Code says it is using the correct interpreter. I can see on the bottom left that it says Python 3.6.1 (virtualenv)

If I want to test the project_root/sample/client/EDAMTest.py code within terminal I can do it via export PYTHONPATH=../../lib; python EDAMTest.py while being in folder project_root/sample/client/.

Now if I am in VS Code, open the file EDAMTest.py, pylint is telling me that it cannot import modules from lib.

Now my question:

How can I add lib to PYTHONPATH in VS Code?

I found several possible ways to do so:

  1. Create a .env file (see [1] below).
  2. Specify PYTHONPATH in .vscode/launch.json file (see [2])

None of the possible solutions I found seem to work.

What am I missing?


[1] Environment variable definitions file

This tells me how to define global (env) vars. So I specified this:

PYTHONPATH="~/.virtualenvs/evernote/bin/python;lib"

But it won't work. Still libs path is not found by pylint


[2] So I did create a launch.json file like so:

{
    "name": "Python",
    "type": "python",
    "request": "launch",
    "stopOnEntry": false,
    "pythonPath": "${config.python.pythonPath}",
    "program": "${file}",
    "cwd": "${workspaceRoot}",
    "debugOptions": [
        "WaitOnAbnormalExit",
        "WaitOnNormalExit",
        "RedirectOutput"
    ],
    "env": {
        "PYTHONPATH": "~/.virtualenvs/evernote/bin/python:lib"
    }
}

---

EDIT

Here is a link that tries to address this problem:

Troubleshooting linting

That link tries to address several possible problems, one is this:

... unable to import

The suggested solution is:

Ensure that the pythonPath setting points to a valid Python installation where Pylint is installed.

=> Yes, I did.

Alternately, set the python.linting.pylintPath to an appropriate version of Pylint for the Python interpreter being used.

=> I did, still no success:

My .vscode/settings.json:

{
    "python.pythonPath": "~/.virtualenvs/evernote/bin/python",
    "python.linting.pylintPath": "~/.virtualenvs/evernote/bin/pylint"
}
like image 520
Ugur Avatar asked Aug 29 '18 14:08

Ugur


1 Answers

It seems that I had to use a colon instead of a semicolon in .env file like so: PYTHONPATH="~/.virtualenvs/evernote/bin/python:lib". That seems to solve the problem.

like image 81
Ugur Avatar answered Oct 05 '22 23:10

Ugur