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

Can't resolve relative imports #124

Closed
spindensity opened this issue May 12, 2019 · 8 comments
Closed

Can't resolve relative imports #124

spindensity opened this issue May 12, 2019 · 8 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@spindensity
Copy link

Pyright can't resolve relative imports as below correctly.

They look like the same problems as #58 and #59, maybe there're some regressions.

from . import some_class
from .some_module import some_func

Pyright 1.0.27 with VS Code 1.33.1 and Python 3.7 virtual environment.

@erictraut
Copy link
Collaborator

Can you provide more repro steps? What other files or subdirectories are present in the same directory as this file?

@spindensity
Copy link
Author

After some digging, they are not the problems as #58 and #59. These problems only happen when there's pyrightconfig.json in the project root folder. Without that config file, everything is OK.

Minimal test case:

Project structure:
01

main.py:

if __name__ == "__main__":
    pass

some_package/__init__.py:

class some_class:
    pass

some_package/module1.py:

def some_func():
    pass

some_package/module2.py:

from . import some_class
from .module1 import some_func


some_obj = some_class()
some_func()

pyrightconfig.json:

{
    "venvPath": "C:/Dev/BasicTools/python/miniconda3/envs",
    "venv": "datascience"
}

The virtual environment:
02

Result:
03
04

@erictraut
Copy link
Collaborator

Thanks for the details, but I'm still not able to repro what you're seeing. There must be something else I'm missing. Could you open the Output window and look at the Pyright output? There should be some additional information provided there. Look for something like "Attempting to resolve relative import". I'm interested in the lines that follow. Thanks!

@erictraut erictraut added the bug Something isn't working label May 13, 2019
@spindensity
Copy link
Author

OK.

Pyright output:

Pyright language server starting
Received updated settings
Loading configuration file at c:\Dev\Projects\github-bug-reports\pyright\bug_id_124\pyrightconfig.json
The venvPath has been specified in both the config file and the VS Code settings. The value in the config file (C:\Dev\BasicTools\python\miniconda3\envs) will take precedence
Searching for source files
No source files found.
Could not import '.' in file '/c%3A/Dev/Projects/github-bug-reports/pyright/bug_id_124/some_package/module2.py'
  Attempting to resolve relative import
  Attempting to resolve using root path ''
  Partially resolved import with directory ''
Could not import '.module1' in file '/c%3A/Dev/Projects/github-bug-reports/pyright/bug_id_124/some_package/module2.py'
  Attempting to resolve relative import
  Attempting to resolve using root path ''
  Could not find directory 'module1'
  Did not find file 'module1.pyi' or 'module1.py'

@erictraut
Copy link
Collaborator

OK, thanks. It looks like it's a Windows-specific path issue. That explains why I wasn't seeing it, since my main development machine is a Mac. I'll switch over to a Windows machine and try to repro it there.

@spindensity
Copy link
Author

spindensity commented May 13, 2019

Thank you.

After checking the output of pyright, it's obvious that these problems are still here without the config file, they just can't be seen from the editor panel but the output message is the same.

Pyright output message without the config file:
05

@erictraut
Copy link
Collaborator

Found the problem. As I suspected, it had to do with processing Windows-specific file paths with the old DOS-style drive letter at the start. I've switched to a standard library for doing the parsing in a cross-platform way. This should be fixed in the next version of pyright.

Thanks again for reporting the bug!

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label May 13, 2019
@erictraut erictraut reopened this May 13, 2019
@erictraut
Copy link
Collaborator

This should now be fixed in version 1.0.28, which I just published. Thanks for reporting the issue!

heejaechang added a commit to heejaechang/pyright that referenced this issue Nov 3, 2021
d4c0c0c made LS to automatically add "src" as one of extra path to find imports. (microsoft#552)

git-subtree-dir: server/pyright
git-subtree-split: d4c0c0c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants