moulip
moulip

Reputation: 151

Python modules unresolved in VScode

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 ?

Upvotes: 1

Views: 1135

Answers (2)

moulip
moulip

Reputation: 151

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 !

Upvotes: 2

Jill Cheng
Jill Cheng

Reputation: 10372

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"
    },

Upvotes: 0

Related Questions