1

I have an issue with VScode I can't figure out. When I began to develop my project I had the following structure :

TopDirectory
 file1.py
 file2.py
 file3.py
 ....

Everything was working fine. Then I decided to put all the file*.py into a subdirectory called core.

TopDirectory
 core
  file1.py
  file2.py
  file3.py

When I execute the code it runs without any issues but Vscode tells me that it can't resolve the modules although they are all in the same (like before). If I import the modules this way : import core.file2 core.file3 in file1, VScode stops complaining but my code does not run any longer telling me that there is no core module when I run file1.py. I also tried the import .file2, VSCode is still happy but Python tells me that there is no known parent package.

Can you help me fix this, or at least understand what's happening here ?

2 Answers 2

2

I have managed to fix the issue. In my project root folder I have created a .vscode/settings.json file. I have added the following statement into the file :

"python.analysis.extraPaths":["./core"]

Now everything is working fine !

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

Comments

0

Based on your description, I created a similar project.

Since these three files are stored in the same folder, and VSCode searches for files from the parent file('core') of the current file, we can use "import file_name" to import other files:

enter image description here

My environment:

OS: Windows_NT x64

VSCode: 1.50.1 (user setup)

"python.languageServer": "Microsoft",

Update:

When I use "python.languageServer": "Pylance", the terminal will display the warning "Import "file2" could not be resolved", but the code result can be output, so I set the following:

enter image description here

or added the following settings in the settings.json:

"python.analysis.diagnosticSeverityOverrides": {
      "reportMissingImports": "none"
    },

10 Comments

Hi and thanks for trying to help me out. I think the difference reside in the fact that I first began to develop the project without the core subdirectory. I'm running VSCode under Debian GNU/Linux with the Pylance Python server.
Something to add though, if I open the core folder alone in VSCode, there is no import problem anymore.
@moulip -When you use the import statement used in my answer, could the result be successfully output? When I use the "Pylance" language service, the code can be executed.
@moulip -I updated my answer and you can refer to it.
I have seen your workaround. But it doesn't really help me. Indeed disabling the reportmissisingimports only disables the warning, but I can't benefit from VScode autocompletion because it actually can't find it which is a bigger issue than the waring itself. But I can see now that the problem is linked to Pylance.
|

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.