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

pyright incorrectly adds the script directory to the module search path #7226

Closed
DetachHead opened this issue Feb 8, 2024 · 4 comments
Closed
Labels
as designed Not a bug, working as intended bug Something isn't working

Comments

@DetachHead
Copy link

# foo/a.py
a = 1
# foo/b.py
import a # no pyright error
# main.py
import foo.b

running python main.py will cause the following error:

> python main.py
Traceback (most recent call last):
  File "C:\project\main.py", line 4, in <module>
    import foo.b
  File "C:\project\foo\b.py", line 4, in <module>
    import a  # no error
    ^^^^^^^^
ModuleNotFoundError: No module named 'a'
@DetachHead DetachHead added the bug Something isn't working label Feb 8, 2024
@erictraut
Copy link
Collaborator

Pyright is working as designed here. This isn't a bug.

Refer to this documentation for details.

@erictraut erictraut closed this as not planned Won't fix, can't repro, duplicate, stale Feb 8, 2024
@erictraut erictraut added the as designed Not a bug, working as intended label Feb 8, 2024
@DetachHead
Copy link
Author

@erictraut that's not related to this issue. the problem is that when running a python file as a module, the imports are not relative to the location of the file.

# foo/a.py
a = 1
# foo/b.py
import a # no pyright error
# should be `import foo.a`
> python -m foo.b
ModuleNotFoundError: No module named 'a'

@erictraut
Copy link
Collaborator

See this documentation (item 6) that describes why this behavior is intended.

@DetachHead
Copy link
Author

there should be an option to disable that behavior imo, since many projects do not intend for all of their python files to be executed as scripts like that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
as designed Not a bug, working as intended bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants