How to Fix Python Pylance Error "Import "X" could not be resolved from source"
When using Visual Studio Code (VS Code) with the Pylance language server for Python development, you might encounter the error message Import "module_name" could not be resolved from source (reportMissingModuleSource)
. This is not a Python runtime error (ModuleNotFoundError
), but rather an indication from Pylance that it cannot find the source code for the imported module within the currently selected Python environment.
This guide explains the common reasons for this Pylance-specific error and provides step-by-step solutions focusing on package installation and interpreter configuration.
Understanding the Error: Pylance Static Analysis
Pylance is a language server that provides features like IntelliSense (autocompletion), code navigation, and error checking for Python within VS Code. It performs static analysis, meaning it analyzes your code without running it. The "Import ... could not be resolved" error means Pylance searched the site-packages
directory (and other relevant paths) associated with the Python interpreter currently selected in VS Code, but it couldn't find the source files for the module you're trying to import.
This is different from a ModuleNotFoundError
which occurs only when you run the Python script and the interpreter itself cannot find the module at runtime. Pylance often flags this issue before you even run the code.
Primary Cause 1: Module Not Installed (in the Selected Environment)
The most common reason is that the required package (e.g., requests
, numpy
, pandas
) is simply not installed in the specific Python environment (global environment or virtual environment) that VS Code is currently configured to use for your project.
Solution 1: Install the Missing Module using pip/conda
Identify the correct package name (it might differ slightly from the import name, though often it's the same) and install it using the appropriate package manager for the environment VS Code is using.
- Activate Environment (If Applicable): If you use a virtual environment (
venv
,conda env
), make sure it's activated in your terminal before running the install command. - Install using
pip
:# General syntax (use pip or pip3 as appropriate for your env)
pip install package_name
# Recommended: Use python -m pip to ensure correct pip for the active python
python -m pip install package_name
# Example: Install 'requests'
pip install requests
python -m pip install requests - Install using
conda
(If using Anaconda/Miniconda):Replace# Activate conda environment first: conda activate your_env_name
conda install package_name
# Often better to specify a channel like conda-forge
conda install -c conda-forge package_name
# Example: Install 'requests' via Anaconda channel
conda install -c anaconda requestspackage_name
with the actual package you need (e.g.,requests
,numpy
,opencv-python
,pandas
).
Primary Cause 2: Incorrect Python Interpreter Selected in VS Code
Even if you've installed the package correctly in an environment, VS Code/Pylance might be looking at a different Python interpreter where the package is not installed. This is very common when multiple Python versions or virtual environments exist.
Solution 2: Select the Correct VS Code Interpreter
You need to tell VS Code which Python executable Pylance should use for analysis.
Using the Command Palette
- Open the Command Palette:
Ctrl+Shift+P
(Windows/Linux) orCmd+Shift+P
(macOS). - Type
Python: Select Interpreter
and press Enter. - A list of discovered Python interpreters (global installs, virtual environments) will appear.
- Crucially, select the interpreter corresponding to the environment where you installed the missing package. This is often one located inside a
venv
or.venv
folder within your project if you're using virtual environments. VS Code usually marks recommended interpreters (e.g., associated with a workspace folder).
Selecting Interpreter Path Manually
If the desired interpreter isn't listed automatically:
- Open the Command Palette (
Ctrl+Shift+P
/Cmd+Shift+P
). - Type
Python: Select Interpreter
. - Choose "Enter interpreter path...".
- Click "Find...".
- Navigate to and select the
python
(Linux/macOS) orpython.exe
(Windows) executable file within the correct environment (e.g., inside thevenv/bin
orvenv\Scripts
folder of your virtual environment, or the main installation path if using global). Click "Select Interpreter".
After selecting the correct interpreter, Pylance will re-analyze your project, and the error should disappear if the package is installed in that environment.
Best Practice: Using Virtual Environments
Using virtual environments is the standard way to manage project dependencies and avoid conflicts between projects or with the global Python installation.
Creating and Activating
# Create (in your project folder)
python3 -m venv venv # Or python / py
# Activate
source venv/bin/activate # Linux/macOS
venv\Scripts\activate.bat # Windows cmd
venv\Scripts\Activate.ps1 # Windows PowerShell
Installing Modules Inside
With the environment activated (e.g., (venv)
appears in your prompt), install packages:
pip install package_name
Packages installed now go into the venv
folder, specific to this project.
Selecting the Venv Interpreter in VS Code
Use the "Python: Select Interpreter" command (as described in Solution 2) and choose the Python interpreter located inside your venv
folder.
Other Potential Causes and Fixes
-
Restart VS Code: Sometimes Pylance or VS Code caches get stuck. A simple restart (
File
>Exit
and reopen) can often resolve temporary glitches. -
Check for Filename Shadowing: Ensure you haven't accidentally named one of your project files the same as the module you're trying to import (e.g., don't name your file
requests.py
if you want toimport requests
). Rename your file if necessary. -
Verify Installation (
pip show
): Double-check that the package is actually installed in the environment VS Code is using. Activate the environment in your terminal and run:pip show package_name
# Or:
python -m pip show package_namenoteIf it says "Package(s) not found," it's not installed there. Check the
Location:
if it is found. -
Reinstall/Upgrade Package: Occasionally, a corrupted installation can cause issues. Try reinstalling:
pip uninstall package_name
pip install package_name
# Or upgrade:
pip install --upgrade package_name
Workaround (Discouraged): Suppressing the Warning (# type: ignore
)
You can silence the Pylance warning for a specific import line by adding a comment:
import requests # type: ignore
import some_other_module # type: ignore[import] # More specific suppression
This does not fix the underlying problem. It merely hides the warning in VS Code. Your code will still fail at runtime with a ModuleNotFoundError
if the module isn't actually installed correctly. Use this only as a last resort or temporary measure if you are certain the module will be available at runtime through means Pylance can not detect (which is rare for standard imports).
Conclusion
The Pylance error Import "X" could not be resolved from source
is primarily an issue of environment configuration and package installation visibility for the language server.
The most common solutions are:
- Install the missing package (
pip install package_name
orconda install package_name
) into the correct Python environment (ideally a virtual environment). - Ensure VS Code is configured to use that same Python interpreter via the "Python: Select Interpreter" command.
- Use virtual environments consistently to manage dependencies effectively.
By ensuring the package is installed where Pylance is looking, you can resolve this common static analysis error and benefit from accurate IntelliSense and error checking.