简体   繁体   中英

VSCode Unable to import 'example' pylint(import-error)

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)

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM