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

Pylance cannot resolve the import of a local .py file #519

Closed
egeres opened this issue Oct 25, 2020 · 16 comments
Closed

Pylance cannot resolve the import of a local .py file #519

egeres opened this issue Oct 25, 2020 · 16 comments
Labels
waiting for user response Requires more information from user

Comments

@egeres
Copy link

egeres commented Oct 25, 2020

Environment data

  • Language Server version: 2020.10.2
  • OS and version: W10 Version 1903
  • Python version: 3.7.5 64-bit

Expected behaviour

When importing a local python script instead of a module, Pylance should be able to read the methods defined in that file. Or at least recognize the import as something valid.

Actual behaviour

Pylance throws the error Import "my_utils" could not be resolved instead of recognizing the file.

Code Snippet / Additional information

Pylance_error

@egeres
Copy link
Author

egeres commented Oct 25, 2020

Apparently Pylance does read local Python files. My issue comes from the structure of the project being as follows:

root/
    folder_0/
        main.py
        my_utils.py

Pylance reads the script when it's placed under the root folder, instead of folder_0. However, if my_utils.py is placed in that location, running main.py with the path as follows: D:/folders/root/ gives an error. The error does not occur when both main.py and my_utils.py are under the same subfolder.

Maybe Pylance should be able to recognize an import of a local file from a subfolder, instead of this file needing to exist in the root of the project ?

@jakebailey
Copy link
Member

jakebailey commented Oct 26, 2020

See the troubleshooting doc: https://github.com/microsoft/pylance-release/blob/master/TROUBLESHOOTING.md#unresolved-import-warnings

You can choose the import roots with extraPaths, open the subfolder, or use a multi-root workspace to split out the roots.

Also related is #253.

@jakebailey jakebailey added the waiting for user response Requires more information from user label Oct 26, 2020
@github-actions github-actions bot removed the triage label Oct 26, 2020
@Promethian3
Copy link

Promethian3 commented Nov 18, 2020

I'm actually having the same issue;
pylance recognizes a local .py file if I import something or everything from it (using relative import), but it doesn't recognize the file when I do a simple import.

image


EDIT:

I followed @jakebailey 's link and followed through the instructions, solving the issue.
Thanks!

@geekgao
Copy link

geekgao commented Nov 29, 2020

Too much troubles.
I think there must be a better solution like before.

@AndreasLuckert
Copy link

I'm still having this error, so I filed a question on StackOverflow. Thanks in advance for your consideration.

@jakebailey
Copy link
Member

I believe you filed #724 for your specific issue.

As far as I can tell, the issues in this thread were solved by setting extraPaths. See also #253 for general issues importing things from scripts (where import roots may differ). I'm going to close this to avoid this issue becoming a catch-all for instances where imports cannot be resolved (which makes giving solutions or finding bugs very difficult).

@cesarvargas00
Copy link

I'm actually having the same issue;
pylance recognizes a local .py file if I import something or everything from it (using relative import), but it doesn't recognize the file when I do a simple import.

image

EDIT:

I followed @jakebailey 's link and followed through the instructions, solving the issue.
Thanks!

I think that relative imports should be the way to go, in this case. Is there a good case where importing without the .(dot) would be a good practice?

@jakebailey
Copy link
Member

The whole "relative" versus "absolute" is a big style war in Python, I think.

If you're looking for distinguishers besides "it works", relative makes it easier to refer to things with long names or unclear roots, but absolute means you can move files without having to update everything. I'm sure there are other reasons I'm missing.

@ror3d
Copy link

ror3d commented Apr 28, 2021

If the "relative" import is the preferred way for the linter, then it should tell you that as an option to resolve the issue. Adding the "python.analysis.useImportHeuristic": true that's mentioned in #253 solved the warnings for me, but apparently this is a hidden setting so that's not really that helpful.

@jakebailey
Copy link
Member

jakebailey commented Apr 28, 2021

Pylance isn't a linter and isn't in the business of preferring one style of import over another or suggesting you use a particular style.

Relative imports are just easier to resolve, as they require no context except the location of the current file. Absolute imports require knowing where the code is run (how imports are rooted), hence extraPaths.

The heuristic is an experiment we're making to try and help fix cases where users haven't set extraPaths, but expect imports to resolve. It's not intended to be exposed until we're confident in the method and have a chance to figure out how it's going to look when finalized.

@uloga
Copy link

uloga commented May 19, 2021

After years started with python and run into so much trouble for nothing! Remember years back when python dev used to be easy and fun to work with. To maintainer of this extension One star! So off putting ( waste of time )!

@andrescastillo08
Copy link

I am able to resolve by closing vs code all the way, and reopening the desired project folder.
Seems like the language server was possibly looking at some relative path to some other  folder I had open previously.

@OsamaAlAhmadieh
Copy link

I tried editing the extra paths setting, error disappeared, but none of the autocomplete features worked.
One interesting thing that happens is the following:
currently i'm using i3wm for linux, and having this problem when i use vscode. however, when i switch back to gnome, the problem completely disappears, and pylance works just fine. I'm still somehow a beginner, I couldn't tell if this is a problem form my side or not. I compared the settings on both wm styles and they were the same. Not sure what to do here, i really like working with i3, so i want to solve this issue.

@sherzodurinov
Copy link

import aiogram could not be resolved Pylance

@zero8d
Copy link

zero8d commented Jul 28, 2022

import aiogram could not be resolved Pylance venv vs code

@raindata5
Copy link

For me src wasn't automatically detected and it had to be added in as in the following manner

    "python.analysis.extraPaths": [
        "./src"
    ],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for user response Requires more information from user
Projects
None yet
Development

No branches or pull requests