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.
Videos
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 have been trying to learn python and it's going well I watched something on how to import from different folders my imports look like
import sys
sys.path.insert(1,"D:\programing\python\learn\book") from book import Book import booksSDK
Book is white text and it's normally green
booksSDK has a yellow squiggle
the program runs in fact there is nothing wrong with it, it does what it should no problems, but VScode complaining at me is driving me nuts.
under problems tab it says
import "booksSDK.py" could not be resolved Pylance(reportmissingimports) ln 4 col 8
the line it is citing is " import booksSDK"
thanks for the help this is kinda driving me a biit nuts
I had the same problem but with all kinds of packages.
My solution was to go to the VSCode settings and search for "python.analysis.extraPaths", and add the path to your site-packages.
In my case, I added C:\Code\Python39\Lib\site-packages, and now it's working fine.
tldr;
TensorFlow defines some of its modules in a way that pylint & pylance aren't able to recognize. These errors don't necessarily indicate an incorrect setup.
To Fix:
- pylint: The pylint warnings are safely ignored.
- Intellisense: The best way I know of at the moment to fix Intellisense is to replace the imports with the modules they are aliasing (found by importing alias in a repl as
xthen runninghelp(x)). Because the target of the alias in my case is an internal name, you probably don't want to check in these changes to source control. Not ideal.
Details
Regarding the linting: It seems that tensorflow defines its modules in a way that the tools can't understand. Also, it appears that the package is an alias of some kind to another package. For example:
import tensorflow.compat.v1 as tf
tf.estimator.RunConfig()
The above code gives the pylint warning and breaks intellisense. But if you manually import the above in a REPL and run help(tf), it shows you the below package, which you can use instead:
import tensorflow_core._api.v1.compat.v1 as tf
tf.estimator.RunConfig()
This second example does not cause the pylint warning. Also the Intellisense features (Go to definition, Ctrl+Click, etc) work with this second example.
However, based on the _api, it looks like that second package name is an internal namespace, so I'm guessing it is probably best to only use this internal name for local debugging.
Confirmation/Tickets
- pylint: I've found a ticket about pylint having issues with a couple tensorflow imports that looks related.
- Intellisense: I've opened a ticket with pylance.
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.
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.