-
Notifications
You must be signed in to change notification settings - Fork 765
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
Comments
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. |
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:
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 |
If this is still a problem for you, you can workaround it by simply adding:
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:
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. |
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 |
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. |
I got it fixed tho. But thanks! |
How? |
https://dev.to/climentea/how-to-solve-pylance-missing-imports-in-vscode-359b |
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"
],
} |
name 'admin' is not defined |
Environment data
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
Code Snippet / Additional information
helloworld.zip
The text was updated successfully, but these errors were encountered: