Last updated: Apr 10, 2024
Reading timeยท5 min
The error "Import "X" could not be resolved from source Pylance" occurs when the imported module is not installed or you have selected the incorrect Python interpreter in your IDE (e.g. Visual Studio Code).
To solve the error, install the module and select the correct Python interpreter in your IDE.
Import "requests" could not be resolved from source Pylance(reportMissingModuleSource) [Ln 1, Col 8]
Make sure you have the specific module installed. You can type "pypi module-name" in Google to get instructions on the installation command.
Click on the Pypi page of the package and look at the installation command.
Here is an example of how I'd install the requests module.
# ๐๏ธ In a virtual environment or using Python 2 pip install requests # ๐๏ธ For python 3 (could also be pip3.10 depending on your version) pip3 install requests # ๐๏ธ If you get a permissions error pip install requests --user # ๐๏ธ If you don't have pip in your PATH environment variable python -m pip install requests # ๐๏ธ For python 3 (could also be pip3.10 depending on your version) python3 -m pip install requests # ๐๏ธ Using py alias (Windows) py -m pip install requests # ๐๏ธ For Anaconda conda install -c anaconda requests # ๐๏ธ For Jupyter Notebook !pip install requests
requests
with the actual name of the module you're trying to install.If the error persists, get your Python version and make sure you are installing the package using the correct Python version.
python --version
For example, my Python version is 3.10.4
, so I would install the requests
package with pip3.10 install <package-name>
.
pip3.10 install requests
Notice that the version number corresponds to the version of pip
I'm using.
If the error persists, make sure your IDE is using the correct version of Python.
If you have multiple Python versions installed on your machine, you might have installed the package using the incorrect version or your IDE might be set up to use a different version.
For example, In Visual Studio Code you can:
CTRL + Shift + P
or (โ
+ Shift
+ P
on macOS) to open the
command palette.Then type "Python select interpreter" in the search field.
If the error persists, try restarting your IDE and development server/script. VSCode often glitches and a reboot resolves the issue.
If the error is not resolved, try to use the Visual Studio Code terminal to install the module.
You can press CTRL + ` (Backtick) on your keyboard to open the Visual Studio code terminal.
CTRL+Shift+P
and then type "View: Toggle Terminal".Once you open the terminal, Visual Studio Code will automatically activate your virtual environment (if you have one).
Run the pip install <module-name>
command.
pip install requests
requests
with the actual name of the module you're trying to install.If the error persists, try to select the Python interpreter by specifying the path:
CTRL + Shift + P
or (โ
+ Shift
+ P
on Mac) to open the command
palette.venv
folder,
then double-click on the Scripts
folder, select the python.exe
file and
then Select interpreter.venv
folder, then double-click on the bin
folder, select the python
file and
then Select interpreter.python.exe
or python
executable, specify the path to
the file and select the executable.where python python -c "import os, sys; print(os.path.dirname(sys.executable))"
If the error persists, try restarting your IDE and development server/script.
pip install <module-name>
command with the virtual environment
active.# ๐๏ธ Use the correct version of Python when creating VENV python -m venv venv # ๐๏ธ Activate on Unix or MacOS source venv/bin/activate # ๐๏ธ Activate on Windows (cmd.exe) venv\Scripts\activate.bat # ๐๏ธ Activate on Windows (PowerShell) venv\Scripts\Activate.ps1 # ๐๏ธ install the specific package in the virtual environment pip install requests
requests
with the actual name of the module you're trying to install.If the python -m venv venv
command doesn't work, try the following 2 commands:
py -m venv venv
python3 -m venv venv
Your virtual environment will use the version of Python that was used to create it.
module_name
For example, if you were trying to import the requests
module, make sure you
haven't named a module in your project as requests.py
because that would
shadow the original requests
module.
You also shouldn't be declaring a variable named requests
as that would also
shadow the original module.
If the error is not resolved, try to uninstall the package and then install it.
# ๐๏ธ Check if you have requests installed pip show requests # ๐๏ธ Uninstall requests pip uninstall requests # ๐๏ธ If you don't have pip set up in PATH python -m pip uninstall requests # ๐๏ธ install requests pip install requests # ๐๏ธ If you don't have pip set up in PATH python -m pip install requests
requests
with the actual name of the module you're trying to install.Try restarting your IDE and development server/script.
You can also try to upgrade the version of the requests package.
pip install requests --upgrade # ๐๏ธ If you don't have pip set up in PATH python -m pip install requests --upgrade
You can check if you have the module installed by running the
pip show <module-name>
command.
# ๐๏ธ Check if you have requests installed pip show requests # ๐๏ธ If you don't have pip set up in PATH python -m pip show requests
The pip show <module-name>
command will either state that the package is not
installed or show a bunch of information about the package, including the
location where the package is installed.
If the package is not installed, make sure your IDE is using the correct version of Python.
If none of the suggestions helped, you can use a comment to disable the Pylance warning in your IDE.
import requests # type: ignore print(requests)
You simply have to add the # type: ignore
command on the same line as the
import statement to disable the check for the specific import.