If files helloworld.py and hello_world.py have identical contents, then Pylance should treat "import helloworld" and "import hello_world" identically as well.
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.
[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)
# 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()
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.
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.

i meet this problems too , and i had uninstell pylance !
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
@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.
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.
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
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.
Most helpful comment
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:
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.