Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reportMissingImports when import a file whose name contains an underscore #52

Closed
RussellJQA opened this issue Jul 4, 2020 · 16 comments
Closed
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@RussellJQA
Copy link

RussellJQA commented Jul 4, 2020

Environment data

  • Language Server version: 2020.6.1
  • OS and version: Windows 8.1 Pro and Windows 10 2004 Home
  • Python version (& distribution if applicable, e.g. Anaconda): 3.8.3 64-bit, no virtual environment

Expected behaviour

If files helloworld.py and hello_world.py have identical contents, then Pylance should treat "import helloworld" and "import hello_world" identically as well.

Actual behaviour

Pylance reports no error for "import helloworld", but (under the conditions explained below) for "import hello_world" it reports:
Import "hello world" could not be resolved
Pylance (reportMissingImports) [1,8]

In C:\Projects\importtests, I have a helloworld subfolder. The subfolder contains 3 files: helloworld.py, hello_world.py, and callhelloworld.py. callhelloworld imports the other 2 files:
• When I open VSCode (using Windows Explorer's context menu) from within C:\Projects\importtests\helloworld, then I do NOT see this problem.
• When I instead open VSCode from within C:\Projects\importtests, then I DO see this problem

I originally saw this problem while doing (as a student) exercises from the Python track of exercism.io.
• The provided unittest files which import a filename containing an underscore ("_") exhibit this problem. [See https://github.com/exercism/python/blob/master/exercises/hello-world/hello_world_test.py.]
• Those importing only filenames without underscores don't exhibit this problem. [See https://github.com/exercism/python/blob/master/exercises/raindrops/raindrops_test.py.]

I did not see this problem with my previous (to Pylance) language server.

Logs

[FG] parsing: c:\Projects\importtest\helloworld\callhelloworld.py (22ms)
[FG] parsing: c:\Users\rljohnson\.vscode\extensions\ms-python.vscode-pylance-2020.6.1\server\typeshed-fallback\stdlib\2and3\builtins.pyi (419ms)
[FG] binding: c:\Users\rljohnson\.vscode\extensions\ms-python.vscode-pylance-2020.6.1\server\typeshed-fallback\stdlib\2and3\builtins.pyi (229ms)
[FG] binding: c:\Projects\importtest\helloworld\callhelloworld.py (1ms)
[BG] analyzing: c:\Projects\importtest\helloworld\callhelloworld.py ...
[BG]   parsing: c:\Projects\importtest\helloworld\callhelloworld.py (74ms)
[BG]   parsing: c:\Users\rljohnson\.vscode\extensions\ms-python.vscode-pylance-2020.6.1\server\typeshed-fallback\stdlib\2and3\builtins.pyi (457ms)
[BG]   binding: c:\Users\rljohnson\.vscode\extensions\ms-python.vscode-pylance-2020.6.1\server\typeshed-fallback\stdlib\2and3\builtins.pyi (202ms)
[BG]   binding: c:\Projects\importtest\helloworld\callhelloworld.py (1ms)
[BG]   checking: c:\Projects\importtest\helloworld\callhelloworld.py (1ms)
[BG] analyzing: c:\Projects\importtest\helloworld\callhelloworld.py (735ms)

Code Snippet / Additional information

# File hello_world.py:
def hello():
	return "Hello, World!"

# File helloworld.py:
def hello():
	return "Hello, World!"

# File callhelloworld.py:	
import hello_world
import helloworld

hello_world.hello()
helloworld.hello()

helloworld.zip

@erictraut
Copy link
Contributor

Pylance doesn't know which search paths will be used at the time you execute your Python code. You need to tell it. By default, Pylance will assume that the search paths will include the root of the workspace you open. It also automatically adds a subdirectory called "src" if it's present, since it's common practice to place your code within a subdirectory of that name. Any other subdirectories that should be included in the search path must be specified using the "python.analysis.extraPaths" setting.

In your example above, you would want to add the following:

"python.analysis.extraPaths": ["helloworld"]

The reason that "helloworld" is being resolved and "hello_world" is not is that the search paths that you have specified include a directory called "helloworld", and it is being treated as a namespace package.

While investigating your bug report, I did find one bug in Pylance, which I have now fixed. When it detected a namespace package, it was not continuing the scan to find a regular module. The Python spec indicates that regular modules or submodules should be preferred over namespace packages. A fix for this bug will be in the next version of Pylance.

@erictraut erictraut added bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version labels Jul 4, 2020
@RussellJQA
Copy link
Author

RussellJQA commented Jul 6, 2020

Thanks for your helpful explanation. Since CPython itself, my Python linters (prospector within VS Code and pylint outside of it), and my previous VS Code Python language server ("Jedi") didn't complain about this, I hadn't realized importing like this was a problem. But now I understand why it is.

I tried renaming my helloworld folder to mysubfolder, and verified that Pylance complained about both imports. Then I temporarily changed back to "Jedi", and it didn't complain about either of them. But then I temporarily changed my Python language server to "Microsoft", and it complained about both imports, too. So, it seems that Pylance is consistent with how the "Microsoft" Python language server does things.

So far, I've downloaded 13 of Exercism.io's 117 Python exercises, and 7 of them have this problem. That's because for some reason they used dashes in their folder names, but underscores in their filenames. So, folder hello-world contains file hello_world.py, which Pylance complain about importing. To avoid encountering this with future Exercism.io Python exercise files, though, I found there'a an easy enough workaround. As explained in Pylance's README I just created a workspace settings.json to override this warning for my Exercism project:

{
    "python.analysis.diagnosticSeverityOverrides": {
        "reportMissingImports": "none"
    }
}

Pylance will still warn me about this with my own projects. But that will help encourage me to be more specific about my imports (or at least to name my folders and filenames more carefully).

Thanks again.

Pylance complains about both imports, but Jedi does not complain about either

@elisayys
Copy link

elisayys commented Jul 9, 2020

i meet this problems too , and i had uninstell pylance !

@jakebailey
Copy link
Member

This issue has been fixed in version 2020.7.0, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/master/CHANGELOG.md#202070-9-july-2020

@RussellJQA
Copy link
Author

@elisayys

i meet this problems too , and i had uninstell pylance !

If this is still a problem for you, you can workaround it by simply adding:

"python.analysis.diagnosticSeverityOverrides": {
    "reportMissingImports": "none"
}

to your settings.json file.

If, like me, you only want to do that for a certain project, then you can add those lines to a project-level settings.json file (instead of to the main VSCode-wide settings.json file). For a project which doesn't yet have its own project-level settings.json file, you just create a new settings.json file at the root level of your project, containing simply:

{
    "python.analysis.diagnosticSeverityOverrides": {
        "reportMissingImports": "none"
    }
}

For example, to do this only for a project stored at C:\Users\user1\Exercism\python, just create a new C:\Users\user1\Exercism\python\settings.json file consisting only of the lines above.

@Ed1123
Copy link

Ed1123 commented Aug 1, 2020

Pylance doesn't know which search paths will be used at the time you execute your Python code. You need to tell it. By default, Pylance will assume that the search paths will include the root of the workspace you open. It also automatically adds a subdirectory called "src" if it's present, since it's common practice to place your code within a subdirectory of that name. Any other subdirectories that should be included in the search path must be specified using the "python.analysis.extraPaths" setting.

I think Pylance should include the path of the current open python file alongside the workspace root. It is the common behavior I was expecting when switching to it. I need to do imports on different test on different folders. Jedi is still doing the trick for me.

@eliasyishak
Copy link

Pylance doesn't know which search paths will be used at the time you execute your Python code. You need to tell it. By default, Pylance will assume that the search paths will include the root of the workspace you open. It also automatically adds a subdirectory called "src" if it's present, since it's common practice to place your code within a subdirectory of that name. Any other subdirectories that should be included in the search path must be specified using the "python.analysis.extraPaths" setting.

I think Pylance should include the path of the current open python file alongside the workspace root. It is the common behavior I was expecting when switching to it. I need to do imports on different test on different folders. Jedi is still doing the trick for me.

^^ I agree, it's not what most people are used to -- I understand adding the path to directory in your file works but it becomes a problem when you work with multiple projects within the workspace

@jakebailey
Copy link
Member

jakebailey commented Aug 17, 2020

This issue was about a specific bug in the import code affecting modules that contained the character _. I have made #253 to better capture the "script imports" problem as this feedback shouldn't be discussed on old closed issues.

@jacob-02
Copy link

jacob-02 commented Jul 6, 2021

"python.analysis.diagnosticSeverityOverrides": {
        "reportMissingImports": "none"
    }

This code does help remove the error, but it doesn't provide the autocomplete features. I am using OpenCV and on importing cv2, the same error shows up. Any clues on how to fix it?

Screenshot from 2021-07-06 19-54-37

@jakebailey
Copy link
Member

Hiding the warning doesn't fix the issue that we didn't resolve the import, it just hides the warning. We can't analyze modules we can't resolve.

In any case, your issue is definitely not related to this one, which has been closed for some time. We have a few open issues related to cv2; they may be related to what you are seeing.

@jacob-02
Copy link

jacob-02 commented Jul 6, 2021

I got it fixed tho. But thanks!

@roboserg
Copy link

roboserg commented Aug 6, 2021

I got it fixed tho. But thanks!

How?

@jacob-02
Copy link

jacob-02 commented Aug 6, 2021

I got it fixed tho. But thanks!

How?

https://dev.to/climentea/how-to-solve-pylance-missing-imports-in-vscode-359b
Tried this. It helped.

@hdouhua
Copy link

hdouhua commented Oct 8, 2021

I used this setting:

{
    "python.pythonPath": "/path_to_python/python3",
    "python.autoComplete.extraPaths": [
        "/path_to_pip/site-packages"
    ],
    "python.analysis.extraPaths": [
        "/path_to_pip/site-packages"
    ],
}

@goktugerol-dev
Copy link

I use Python Anaconda most of the time and my linux have it's default Python 3.xxx just like most Linux distros. I had this error when I launched a project that I was working on with Anaconda, I just changed the default Python with Conda distro from the IDE and the error gone. I hope this helps.

@AlfyThomas
Copy link

name 'admin' is not defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests