-
Notifications
You must be signed in to change notification settings - Fork 770
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
Comments
Apparently Pylance does read local Python files. My issue comes from the structure of the project being as follows:
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: 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 ? |
See the troubleshooting doc: https://github.com/microsoft/pylance-release/blob/master/TROUBLESHOOTING.md#unresolved-import-warnings You can choose the import roots with Also related is #253. |
I'm actually having the same issue; EDIT:I followed @jakebailey 's link and followed through the instructions, solving the issue. |
Too much troubles. |
I'm still having this error, so I filed a question on StackOverflow. Thanks in advance for your consideration. |
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). |
I think that relative imports should be the way to go, in this case. Is there a good case where importing without the |
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. |
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 |
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 The heuristic is an experiment we're making to try and help fix cases where users haven't set |
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 )! |
I am able to resolve by closing vs code all the way, and reopening the desired project folder. |
I tried editing the extra paths setting, error disappeared, but none of the autocomplete features worked. |
import aiogram could not be resolved Pylance |
import aiogram could not be resolved Pylance venv vs code |
For me
|
Environment data
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
The text was updated successfully, but these errors were encountered: