3

I am getting pylint errors in VSCode that say they are unable to import local files. However, I am able to run the files through the debugger with no problem. I thought that pylint used the same PYTHONPATH that the interpreter uses, so I don't know why this is happening.

I have my code set up like so:

dir0
-dir1
--__init__.py
--src
---__init__.py
---srcdir1
----__init__.py
----file1.py
---srcdir2
----__init__.py
----file2.py

file1.py looks like this:

def func1():
    return 1

file2.py looks like this:

from srcdir1.file1 import func1
func1()

in launch.json I have:

"env": {"PYTHONPATH": "/full/path/to/dir0/dir1/src:/usr/local/bin/python"}

Pylint is giving me an import error around "from srcdir1.file1". When I go into the debugger and click run debugger, the file runs with no issues. However, if I right click and select Run Code, I get import errors that match the pylint errors.

EDIT: I created a file in my workspace folder called .env in my workspace folder. It is as follows:

PYTHONPATH=/Library/Python/2.7/site-packages:/Users/user/path/dir0/dir1/src:/Users/user/path/client/src:/Users/user/path/product/src

Interestingly, I can import from product (the third in the list) but not from client. Is there somewhere that this environment is being overridden?

I also have the following in the file:

import os
import shutil
import sys

For some reason, import sys (but not the others) gives me the following error: unresolved import 'sys'Python(unresolved-import)

jss367
  • 4,759
  • 14
  • 54
  • 76

2 Answers2

0

Do you have __init__.py files inside those folders? Otherwise python won't recognise them as modules and will be unable to import the code. Have a look at https://stackoverflow.com/a/448279/5015356 for more information

Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49
0

The problem is that you specified a PYTHONPATH for the debugger and not the general extension to send to Pylint. Try setting PYTHONPATH in a .env environment variable definition file.

Brett Cannon
  • 14,438
  • 3
  • 45
  • 40
  • I tried that and I'm still getting an error. I edited my post to include more information. Could you take a look? – jss367 Aug 06 '19 at 22:13
  • Two things. One, `/Library/Python/2.7/site-packages` in `PYTHONPATH` is redundant as Python will add that to `sys.path` automatically. Two, your question about `client/` is hard to answer as you didn't share any details about another directory that you were trying to import from and didn't provide the layout details, etc. – Brett Cannon Aug 07 '19 at 16:31