3

I'm running vscode from some project/ folder and getting an "unresolved import" error in some project/impl/ folder. In the impl/ folder I have 2 Python files:

# lib.py
class A():
    pass

# run.py
from lib import A # vscode error here - unresolved import
...

When running run.py the Python interpreter finds the lib just fine but vscode shows an "unresolved import" error (screenshot).

If I change the import line to from .lib import implementation (note the dot), I get the opposite behaviour where vscode resolves the import fine but the Python interpreter fails.

How should I import the lib or otherwise configure vscode to resolve imports from a local folder? (obviously I don't want to add the exact path of the local folder to the vscode config file since I would have to do so for every subfolder in the project)

4
  • Is the parent file of “lib.py” file and "run.py" file both "impl" folder? After I run it according to the information currently provided, it can be imported and used, so could you provide us with a complete file directory structure? Commented Sep 7, 2020 at 3:14
  • To reproduce - run vscode from some folder, create a subfolder and create the two files within the subfolder. I added a screenshot. Commented Sep 7, 2020 at 7:01
  • Once you have a project with subdirectories, it's better to create a (local) python package. Commented Sep 7, 2020 at 7:44
  • @Wups can you please explain further? Commented Sep 7, 2020 at 10:15

2 Answers 2

7

When I used the python extension version 2018.12.1 in my computer, I got the same issue as you described.

Since this function is provided by the language service, and the python language service is provided by the python extension, it is recommended that you could try to use the latest version of the python extension(2020.8.106424).

In addition, you could use the extension "Pylance", which also provides excellent language services for python.

The project I created on my computer:

enter image description here

My environment:

VSCode Version: 1.48.2 (user setup)

OS: Windows_NT x64 10.0.18362

Python extension: 2020.8.106424

languageServer in settings.json:

 "python.languageServer": "Pylance",

Update1:

When the language service I use is "Jedi", the code also does not have this warning:

"python.languageServer": "Jedi",

Update2:

Since this warning does not affect the execution result of the code, we could also turn off this kind of warning in the settings (settings.json):

"python.analysis.disabled": [
      "unresolved-import"
  ]

In this way, we can use "python.languageServer": "Microsoft",

Sign up to request clarification or add additional context in comments.

6 Comments

I am running the latest version of the Python extension and the problem persists. The Pylance extension indeed solves this issue although I need to test what other changes it brings when installing it.
@gilad I updated my answer, and you could try to refer to it. In addition, "Pylance" is an extension developed by Microsoft that can work with python extensions and provide high-performance language support. You can refer to it: Pylance
Jedi seems to resolve the import correctly but whenever I change to jedi, vscode tries to revert back to the Microsoft language server. I wish I could configure the Microsoft language server to resolve these imports as well.
@gilad According to your description I added "Update2" in my answer, and you could refer to it.
Thank you, but the problem is not the underline warning. When the language server fails to resolve the import, it also skips the relevant lines during debugging and will also refuse to "go to definition - F12" on functions and classes defined in the imported file.
|
1

You can add the folder to path of module searching for python using sys module.

import sys
sys.path.insert(1, "./impl/")

from lib import A

Note, the vs code can still underline the import line, but, it will work just fine when you run the program. Give it a try!

You can add more paths like this -

sys.path.insert(n, <path to folder>)

Keep care to use a new natural number in place of n for every new path.

1 Comment

The vscode underline is really my problem (this also has implications in debugging and navigating). The code runs fine otherwise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.