Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Missing (editable) Imports: But python REPL has no problems to import #6376

Closed
buhtz opened this issue Sep 11, 2024 · 8 comments
Closed

Missing (editable) Imports: But python REPL has no problems to import #6376

buhtz opened this issue Sep 11, 2024 · 8 comments
Assignees
Labels
needs repro Issue has not been reproduced yet pep 660 Issues related to PEP 660 import hooks user responded Was "waiting for user response" and they responded

Comments

@buhtz
Copy link

buhtz commented Sep 11, 2024

Environment data

  • Pylance version: v2024.9.1
  • OS and version: Windows 10 (I am forced to)
  • Python version (& distribution if applicable, e.g. Anaconda): Regular python.org Python "'3.11.5 (tags/v3.11.5:cce6ba9, Aug 24 2023, 14:38:34) [MSC v.1936 64 bit (AMD64)]"
  • VSCode 1.93.0

Problem

PyLance reports Import "buhtzology" could not be resolved... but regular python has no problem with it. This works fine and without errors:

python -c "import buhtzology"

Expected behavior

PyLance should use the same "environment" as the regular python interpreter does.

Idea

It might be the case that PyLance is not able to handle packages installed as "--editable" (via python -m pip)
The package install data can be found in the path C:\python\Lib\site-packages\buhtzology-0.2.0.dev0.dist-info. Inside that folder the content of fiule direct_url.json points to the real python file:

{"dir_info": {"editable": true}, "url": "file:///C:/Users/buhtzch/goodCloud/my.work/buhtzology"}

The Python interpreter can handle that without problems. So PyLance should also.

@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Sep 11, 2024
@buhtz buhtz changed the title Missing Imports: But python REPL has no problems to import Missing (editable) Imports: But python REPL has no problems to import Sep 11, 2024
@debonte
Copy link
Contributor

debonte commented Sep 11, 2024

Please see https://microsoft.github.io/pyright/#/import-resolution?id=editable-installs. If the .pth file for buhtzology contains only a path (no Python code), it will work with Pylance.

@debonte debonte added waiting for user response Requires more information from user pep 660 Issues related to PEP 660 import hooks labels Sep 11, 2024
@buhtz
Copy link
Author

buhtz commented Sep 11, 2024

That link does not explain anything and also does not offer a solution.
PyLance is the problem not the settings. The python interpreter (and a lot of other related tools like ruff, pylint, pycodestyle, pytest, codespell, ....) itself does not have a problem using that editable-installed packages. So why does PyLance have a problem with it?

@github-actions github-actions bot added user responded Was "waiting for user response" and they responded and removed waiting for user response Requires more information from user labels Sep 11, 2024
@rchiodo
Copy link
Contributor

rchiodo commented Sep 11, 2024

The key part of that explanation is the 'static' part. Pylance is a static analyzer. It doesn't run python code. Dynamic code in .pth files cannot be evaluated in order to find imports. That's why it doesn't work.

If you use compat mode, where it writes a file path out to the .pth file, that should work.

@buhtz
Copy link
Author

buhtz commented Sep 11, 2024

So pylance parse python code as text file but never runs it?
PyLint also is a static linter but is able to handle editable installed packages.

@rchiodo
Copy link
Contributor

rchiodo commented Sep 11, 2024

I can't say how Pylint handles editable installs, but Pylance does not run any python code. It's not even written in Python.

@buhtz
Copy link
Author

buhtz commented Sep 12, 2024

I can't say how Pylint handles editable installs

Maybe then Pylint would be a good start to investigate a solution for PyLance.

I twas not clear from the previous Issues and not from docs. Do the maintainers of PyLance treat this as a real bug or is this intended behavior? When the "bug" is fixed doesn't matter at this question. Is it a bug: Yes or No?

@rchiodo
Copy link
Contributor

rchiodo commented Sep 12, 2024

As of right now this issue is by design.

Pylint is actually written in Python, so likely can use the import mechanism in the runtime to attempt to import packages. Pyright/Pylance are written in typescript and have to model anything the python runtime does in typescript.

In the past editable installs have always been answered with use compat mode, we don't support dynamic imports, this is by design.

The problem is a lot of people do editable installs so maybe it's time we support some subset of dynamic imports. The problem is that ends up exploding into essentially emulating everything the python runtime does, so we end up back with just running python to find imports.

I'm going to convert this item into a discussion for up votes.

@microsoft microsoft locked and limited conversation to collaborators Sep 12, 2024
@rchiodo rchiodo converted this issue into discussion #6384 Sep 12, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
needs repro Issue has not been reproduced yet pep 660 Issues related to PEP 660 import hooks user responded Was "waiting for user response" and they responded
Projects
None yet
Development

No branches or pull requests

4 participants