The accepted answer won't fix the error when importing own modules.
Use the following setting in your workspace settings .vscode/settings.json:
"python.autoComplete.extraPaths": ["./path-to-your-code"],
Reference: Troubleshooting, Unresolved import warnings
UPDATE 2023
As mentioned by shmim python-language-server is deprecated and new closed source LSP Pylance's setting is configured as follows:
"python.analysis.extraPaths": ["./path-to-your-code"]
Answer from Shinebayar G on Stack OverflowThe accepted answer won't fix the error when importing own modules.
Use the following setting in your workspace settings .vscode/settings.json:
"python.autoComplete.extraPaths": ["./path-to-your-code"],
Reference: Troubleshooting, Unresolved import warnings
UPDATE 2023
As mentioned by shmim python-language-server is deprecated and new closed source LSP Pylance's setting is configured as follows:
"python.analysis.extraPaths": ["./path-to-your-code"]
In your workspace settings, you can set your Python path like this:
{
"python.defaultInterpreterPath": "/path/to/your/venv/bin/python",
}
I'm trying to use torch in a python script but even though it's pip installed, pylance doesn't recognize it
https://imgur.com/EM5fEIo
Any advice on how to resolve this? Thanks.
Import could not be resolved in VS Code
"Import could not be resolved" for local packages and modules
flask - Import could not be resolved/could not be resolved from source Pylance in VS Code using Python 3.9.2 on Windows 10 - Stack Overflow
Visual Studio 2022 import xyz could not be resolved from the source
Videos
Open the Command Palette (Ctrl+Shift+P), then select the Python: Select Interpreter. From the list, select the virtual environment in your project folder that starts with
.env.Run Terminal: Create New Integrated Terminal (Ctrl+Shift+` or from the Command Palette), which creates a terminal and automatically activates the virtual environment by running its activation script.
Install
sqlalchemyandmongoenginewith commandpip install. Once installing them successfully, there will intellisense when you import them and no warnings shown.

Besides, the folder .vscode is to store Workspace settings as well as debugging and task configurations.
To resolve the issue, perform the following steps:
- Open the Command Palette by pressing Ctrl+Shift+P on your keyboard.
- In the Command Palette, select Python: Clear Cache and Reload Window.
The issue was indeed with Pylance. It was missing an "additional path" to where pip had installed the projects I wanted to import. To solve the issue:
First make sure you know the location of your import; you can find it with:
$ python
>>> import modulename
>>> print(modulename.__file__)
Then, once you know the location:
- Open settings (ctrl + ,)
- Search "pylance" or find it under "Extensions > Pylance"
- Find the "Extra Paths" config item
- Use "add item" to a add a path to the parent folder of the module. It will not do any recursive tree searching
And you should be good to go!
For a further example, you can see the image above where I had added the path /home/seph/.local/lib/python2.7/ to no avail. Updating it to /home/seph/.local/lib/python2.7/site-packages/ did the trick.
I had a similar problem in my setup as I create a virtual environment per project and install Jupyter there. Instead of a global setting change in the Pylance extension, I rather prefer to follow what states its troubleshooting guide.
Suppose your virtual environment is called venv and it is inside your project folder:
- Create a folder called
.vscodeat the root of your project folder, if it does not already exist. Don't forget the dot in front of the name. - Create or edit the file
.vscode/settings.jsonand include this tag:
{
"python.analysis.extraPaths": ["./venv"]
}
- Note that the path to your virtual environment is relative to the root project folder, not the folder containing the
settings.jsonfile.
I hope this helps.
I'm using the latest versions of VSCode, Python, and Python extension for VSC. I am running my code in a Virtual Environment as well.
Given an extremely simple folder structure like this:
\dev |_main.py |_other.py
from other import other in main.py is linting as "unresolved import 'other'" on the file name, i.e. the first "other".
The code however runs perfectly, so it isn't a syntax issue.
I'm getting this weird thing in vscode where my import is working properly but theres a yellow line under the module name and when I hover over that, it says module can't be resolved.
If I try and change how I access the module (using a relative path, perhaps), the yellow error line is no longer there but the code doesn't work as python or vscode doesn't recognise the module name/path
Does anyone have experience with this?
My code works but the yellow line is annoying.
I don't NEED to separate my code into different files but it would still be good to know what's going on here
Thank you