pylance-release icon indicating copy to clipboard operation
pylance-release copied to clipboard

'reportMissingModuleSource' warning for requests.packages.*

Open trallnag opened this issue 3 years ago • 10 comments

Bug

Pylance shows a reportMissingModuleSource on an valid import statement. And in addition the autocomplete and hinting works, it is just the warning that is wrong. Interestingly all other import statements that import stuff from the same library (requests) work fine.

image

Environment data

  • Language Server version: 2020.11.0
  • OS and version: Linux, Fedora 32
  • Python version 3.9 with Pyenv + virtualenv

The only installed package in the environment is requests. I ensured that the venv is activated:

image

Here is the pip list output:

Package    Version
---------- ---------
certifi    2020.11.8
chardet    3.0.4
idna       2.10
pip        20.2.3
requests   2.24.0
setuptools 49.2.1
urllib3    1.25.11

How to recreate

You must have some kind of virtual environment tool, for example venv. I will use Pyenv and virutalenv in the example:

In shell:

pyenv virtualenv 3.9.0 test_pylance_bug
pyenv activate test_pylance_bug
pip install requests
code .

Ensure tht correct env is activated.

Create a file on VS Code with following content

from requests.packages.urllib3.util import Retry
from requests.adapters import HTTPAdapter

x = Retry()
a = HTTPAdapter()

trallnag avatar Nov 11 '20 18:11 trallnag

I can confirm the same issue in this environment:

  • Pylance version: 2021.1.3
  • OS and version: Arch Linux (Kernel: Linux 5.10.11-arch1-1)
  • Python version: 3.9.1 with virtualenv (but no Pyenv)

The command from requests.packages.urllib3.util import Retry is actually a shortcut for from requests.packages.urllib3.util.retry import Retry but both return a reportMissingModuleSource warning.

madewild avatar Feb 03 '21 21:02 madewild

@trallnag Hi, thank you for the reporting.

this is happening since those modules are created dynamically.

requests\packages.py looks like this

import sys

# This code exists for backwards compatibility reasons.
# I don't like it either. Just look the other way. :)

for package in ('urllib3', 'idna', 'chardet'):
    locals()[package] = __import__(package)
    # This traversal is apparently necessary such that the identities are
    # preserved (requests.packages.urllib3.* is urllib3.*)
    for mod in list(sys.modules):
        if mod == package or mod.startswith(package + '.'):
            sys.modules['requests.packages.' + mod] = sys.modules[mod]

# Kinda cool, though, right?

but pyi in typeshed define these statically as expected. that's why you are seeing the warning (since typechecker can't verify py files exist for those modules in pyi)

now, how to solve this. unfortunately, it is not easy for us to solve any dynamic behaviors until we have some kinds of plug in mechanism to inject some dynamic behaviors per libraries.

that being said, if you are using default typecheckingmode (which is off), you can import urllib3 directly instead

from urllib3.util import Retry

since all actual library does is re-exporting urllib3 at runtime, it should work fine at runtime, and you should get auto-complete and etc at design/editing time.

unfortunately, if you use other typecheckingmode, you might get type errors since symbol from typeshed pyi and urllib3 will be considered as different types.

heejaechang avatar Mar 19 '21 21:03 heejaechang

To further expand what said before, this works and won't throw another warning for urllib3.disable_warnings(InsecureRequestWarning) not being a declared variable(?)

import requests
from requests.packages import urllib3
from urllib3.exceptions import InsecureRequestWarning
urllib3.disable_warnings(InsecureRequestWarning)

alsitn avatar Aug 20 '21 10:08 alsitn

add the venv folder to your VS Code workspace.

Alex2218 avatar Sep 29 '21 11:09 Alex2218

@alsitn I am not sure what you mean.

heejaechang avatar Jan 19 '22 04:01 heejaechang

I encounter this issue when developing a python extension modules (using Rust). As it is an extension created in another language, the source code is not available and hence I have that warning.

I don't think the warning in this case (extension + already provided pyi) can help much as there is no way to fix (no source code). Can we have a work around for this specific scenario?

binh-vu avatar Jun 19 '22 19:06 binh-vu

If the extension is written in another language, the import should resolve to a binary library file (e.g. a ".so" file on Mac or Linux or a ".pyd" file on Windows). The warning is an indication that pylance would not resolve the import to either a python source (.py) file or a binary library file. Do you see such library files present?

erictraut avatar Jun 19 '22 19:06 erictraut

I'm getting this behavior while straight importing all of requests import requests which produces the following in the problem tab. Import "requests" could not be resolved from source The code works fine and I detect no actual problem other than this annoying false positive. Is there anyway to make this go away. I've got the right environment set and I've pip uninstalled /installed multiple times, I've tried running pip as a standalone command pip install requests as a module in py py -m pip install requests and with a fully qualified python.exe "c:\program files\python310\python.exe" -m pip install requests I've tried to clear the pip cache and to put --force on the install. Always the same outcome. The install works fine, the script runs fine, just pylance complaining about not being able to resolve this.

siggib007 avatar Jul 15 '22 15:07 siggib007

I am having this issue, too. there is no complain in python cell, but when i do: 'import requests' the error message of "import 'requests' could not be resolved from source pylance(missingModuleSource)"

pcplusmac avatar Aug 06 '22 00:08 pcplusmac

I have this issue with random modules.

Modules I've had this warning so far:

django.core.management.utils django.db

Pylance v2022.8.21.

And yes the virtual environment is active.

omidshojaee avatar Aug 11 '22 15:08 omidshojaee

same issue Pylance v2023.3.20

hitrust avatar Mar 12 '23 11:03 hitrust

@hitrust can you open a new issue with a log as described in the troubleshooting guide?

this issue is old one and won't be tracked.

heejaechang avatar Mar 13 '23 17:03 heejaechang