python - Visual Studio: Getting a "reportMissingModuleSource" error, and can't fix - Stack Overflow
'reportMissingModuleSource' warning for requests.packages.*
python - Import "flask" could not be resolved from source Pylance (reportMissingModuleSource) - Stack Overflow
vs code - reportMissingModuleSource with venv
Videos
When I did not install the module "flask" in the Python environment currently used in VSCode:

Please use the command "pip --version" to check the source of the module installation tool "pip", the module is installed at this location:

Then, we can use the command "pip show flask" to check the installation location of the module "flask": (It checks whether the installation location of the module is consistent with the Python environment displayed in the lower left corner of VSCode.)

If the "reportMissingModuleSource" message is still displayed here, please reload VS Code.
(F1, Developer: Reload Window)

Are you using a Virtualenv? If so make sure that VSCode is using the virtualenv as your python interpreter, otherwise it will not be able to pick up the packages that you installed inside this virtualenv.
To do so, click on the Python interpreter in your bottom bar, you should get a list of possible python interpreters including your virtualenv.
Hello ! I'm new to python and i have a problem, im running VSC on my chromebook (i know), and when i want to import modules (like matplotlib or pandas) i get the same error message
"Import "pandas" could not be resolved from source Pylance(reportMissingModuleSource)"
It works fine on my other computer (running Windows) but not on this one. Is it a problem with Linux or something i forgot to do when downloading VSC maybe ?
Pylance requires you to set the Python PATH:
If you're in Mac/Linux, use:
which python3
And in Windows (Command Prompt cmd.exe):
where python
So that the path in which your Python is installed is returned.
Copy that path.
Go to your vscode and open the settings.json file (CTRL + SHIFT + P, and type "settings.json" at search bar)
Add the following key to the json file:
"python.defaultInterpreterPath": "<PATH RETURNED ABOVE>"
The path may look like
"C:\\Users\\YOURUSERNAME\\anaconda3\\bin\\python.exe on Windows or "/usr/local/bin/python3" on Mac/Linux.
Note: backslashes in settings.json must be escaped with an additional backslash, so C:\Users becomes "C:\\Users".
The following documentation provides more information about how to configure Python for Visual Studio Code: https://code.visualstudio.com/docs/python/settings-reference
Also, on some occasions, you might have configured your environment by adding custom paths that Pylance can not detect.
In that case, you can use the python.analysis.extraPaths parameter to add more paths to your project, such as :
"python.analysis.extraPaths": ["app", "another/path/etc"]
(Source: https://dev.to/climentea/how-to-solve-pylance-missing-imports-in-vscode-359b)
I finally figured out what was wrong. There seems to be an open issue with LSP Notebooks experiment on vscode that affects Jupyter Notebooks in the editor. The solution seems to be to set the "python.pylanceLspNotebooksEnabled" flag to False which by default is set to True and it fixes the problem of this error in Jupyter Notebooks in vscode.


This problem only seems to affect the docstring but doesn't affect autocomplete. Thanks to others who tried to help with this question and hope it helps those who might come across the same problem.
In my case the problem was in pyproject.toml file with extraPaths option specified. It seems to have precedence over the python.analysis.extraPaths setting in vs code.
When I did not install the module "flask" in the Python environment currently used in VSCode:

Please use the command "pip --version" to check the source of the module installation tool "pip", the module is installed at this location:

Then, we can use the command "pip show flask" to check the installation location of the module "flask": (It checks whether the installation location of the module is consistent with the Python environment displayed in the lower left corner of VSCode.)

If the "reportMissingModuleSource" message is still displayed here, please reload VS Code.
(F1, Developer: Reload Window)

Are you using a Virtualenv? If so make sure that VSCode is using the virtualenv as your python interpreter, otherwise it will not be able to pick up the packages that you installed inside this virtualenv.
To do so, click on the Python interpreter in your bottom bar, you should get a list of possible python interpreters including your virtualenv.
Noob here. Currently dealing with a very simple yet frustratingly difficult to resolve problem importing python modules.
When using pip install in the terminal I get a Requirement Already Satisfied response in the console, however in code when Import the module, I get the error code pasted at the bottom. I also get this issue if I create a Python file and try to import it in my main.py
I think the issue is the path they're getting installed in is not where VSCode is looking, but I've been unable to find a way to resolve it. Either that, or pip install is using a different instance of python that isn't what VSCode is using? Very annoying. If you have a solution, please let me know! Thanks!
[{
"resource": "/Documents/Coding/VSCode/Projects/Photoeditor/PhotoEditor.py",
"owner": "_generated_diagnostic_collection_name_#0",
"code": {
"value": "reportMissingModuleSource",
"target": {
"$mid": 1,
"external": "https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportMissingModuleSource",
"path": "/microsoft/pyright/blob/main/docs/configuration.md",
"scheme": "https",
"authority": "github.com",
"fragment": "reportMissingModuleSource"
}
},
"severity": 4,
"message": "Import \"requests\" could not be resolved from source",
"source": "Pylance",
"startLineNumber": 2,
"startColumn": 8,
"endLineNumber": 2,
"endColumn": 16
}]
If you want to install a package into your specific virtual environment, you need to first "activate" that environment. Likewise, if you want to run your script in that environment, you need to first "activate" it. You can do this manually or preferably let VSCode handle it for you.
In order to tell VSCode(especially the language server which is pylance) to use that environment:
- Open up your Command Palette(press
ctrl+shift+Porf1) and type :"python: select interpreter". - Browse/Select your newly created python interpreter's path inside your venv.
- Add
"python.terminal.activateEnvironment": trueto yoursetting.jsonfile(It's also possible via GUI setting of course by searching forpython: activate environment). This will automatically detect and activate your venv whenever you open your integrated terminal. Obviously If the path should points to a venv interpreter. (Note: you have to have a Python file opened in your editor).
You can also set your Python's interpreter path manually:
- Create a folder called
.vscodein your workspace. - Create a file called
settings.jsoninside it. - Add these to
settings.json:
{
"python.defaultInterpreterPath": "PATH_TO_VENV_INTERPRETER",
"python.terminal.activateEnvironment": true
}
Note: What I normally do is, I insert a "python.defaultInterpreterPath" key to my User settings.json which points to my global interpreter. Then I create Workspace settings.json for each of my projects and add the same key which points to my venv's interpreter. Remember, workspace settings.json will overwrite user's settings.json.
This way whenever you open VSCode in a project folder it automatically knows it should activate your venv's interpreter(I told it to do so with "python.terminal.activateEnvironment") and if you open VSCode in a normal folder it correctly use your global's interpreter.
Difference between User and Workspace settings.json.
Please select the appropriate interpreter for your running environment.
ctrl+shift+P
and then choose the one like picture.
