简体   繁体   中英

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 ?

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 !

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:

在此处输入图片说明

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:

在此处输入图片说明

or added the following settings in the settings.json :

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

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