Visual Studio Code (VSCode) reports an error (yellow squiggly lines) saying "Import [nameOfModule] could not be resolved by Pylance (reportMissingImports)". You may be using a virtual environment (e.g., venv).
You need to tell Pylance where pip
has installed the module you are trying to import. This is called an "additional path".
- Find the location of the module. In the VSCode terminal within your project enter the python interpreter by typing
python
. - Once the interpreter is active (shown by >>> instead of $ or %) type
import [nameOfModule]
and press enter. - Type
print([nameOfModule].__file__)
to print the path location of the module. - In VSCode open settings by pressing
ctrl + ,
and a search box will appear. - Search for and select "pylance".
- Find the "Extra paths" item.
- Press the "add item" button and copy & paste the path you found in step 3.
The squiggly lines and the error should have gone. Pylance now knows where to find the module you were attempting to import.
N.b. you can exit the python interpreter by typing exit()
.
Thank you very much. This worked perfectly.