Comments (15)
I believe you are hitting this issue (https://microsoft.github.io/pyright/#/import-resolution?id=editable-installs)
unfortunately, currently we don't support the new dynamic setup behavior. you need to install using old legacy mode following legacy structure.
from pylance-release.
.pth
files should be in the site-packages folder for your package I believe.
I think this is the documentation for .pth on python.org
https://docs.python.org/3/library/site.html
When investigating similar issues, that's where I've seen them.
from pylance-release.
it is related to https://peps.python.org/pep-0660/ as @debonte labeled this issue.
Basically, setuptool
used to use a static way (file path in pth) to indicate where a static analysis tool can find the source for the editable install package, the Pep660
changed it to use a script
as you can see import __editable___constructions_0_0_1_finder; __editable___constructions_0_0_1_finder.install()
.
problem of the approach is that something needs to run that script to find out where the source of editable install package is, and most of static analysis tools don't run (including us) random code (since tool can't trust what that install script would do) and there is no guarantee that env is set up properly to run the script for the analysis tool and etc.
due to those various reasons, we don't support pep660 way of editable install. but only legacy compat
or strict
mode.
...
about this,
move src/init.py to src/constructions/init.py and rerun python -m pip install -e . ...
this probably works because we discover them not as editable install
but as regular user files. I think your script is probably in the same workspace as that editable install package is?
if you have your package in root1
and put your script in root2
of vscode multi root workspace
(*.code-workspace) and do editable install
of package
in root1
to root2
, I bet your approach won't work. but it will work with compat
or strict
mode.
...
that said, underneath, all the legacy mode does for us is adding the path in the .pth
in our python search paths (PYTHONPATH
). you can get the same effect by either manually setting PYTHONPATH
environment variable or python.analysis.extraPath
options in vscode settings (settings.json
).
if you don't want to deal with editable install
mechanism and all you want is resolving imports to it in edit time, you can just use python.analysis.extraPath
approach for pylance
and use the new approach for runtime
(when you actually run the script)
if you set PYTHONPATH
manually, that should work for both run time
and edit time
as well.
otherwise, the compat/strict
mode.
hope this helps.
from pylance-release.
@ThiefMaster correct me if I'm wrong, but the impression I got from @heejaechang is that this is really an issue with other tools/standards, EG PEP660, setuptools
, pip
, and that actually this shouldn't be fixed by PyLance (because it would require running external code which is undesirable in a static analysis tool like PyLance), but rather it should be fixed elsewhere (IE in those other tools/standards)
from pylance-release.
I don't know much about what goes on under the hood with Pylance/Pyright/pip install
/setuptools
, but the link you posted describes configuring the editable install to use .pth
files with file paths instead of executable lines and import hooks, EG by using --config-settings editable_mode=strict
.
In my example above, I was able to make Pylance work correctly by changing the location of my source file and removing [tool.setuptools.package-dir]
from pyproject.toml
, but otherwise not changing anything about the way I install the module (IE in the second case Pylance works fine without having to use --config-settings editable_mode=strict
or anything like that), and it's not clear to me that this would change whether pip
/setuptools
uses file paths vs executable lines?
from pylance-release.
Is there a way I can check whether the editable install is using .pth files that contain file paths rather than executable lines? If so I can check in both of the cases I described in the original issue and see if this is indeed the case
from pylance-release.
@heejaechang you are correct! The .pth
file is in /home/username/.local/lib/python3.10/site-packages/__editable__.constructions-0.0.1.pth
.
In the first case, with src/__init__.py
, it reads:
import __editable___constructions_0_0_1_finder; __editable___constructions_0_0_1_finder.install()
In this second case, with src/constructions/__init__.py
, it reads:
/home/username/programming/constructions/src
So you're right, this does change whether the .pth
file that contains file paths vs executable lines
from pylance-release.
It's not clear to me why specifying [tool.setuptools.package-dir]
should change whether file paths are used vs import hooks in the .pth
file, but I suppose that's no longer relevant to Pylance
from pylance-release.
It would be nice if this could be fixed properly. Especially with people starting to use the much faster uv pip
instead of pip
, there's a much higher chance of getting a PEP660 installation (even if setuptools is available).
from pylance-release.
@ThiefMaster I will tag some people @debonte @luabud but I doubt it will be supported in static analysis tool anytime soon. I don't think other tools such as mypy support it for the same reason.
from pylance-release.
related issue #3473
from pylance-release.
Sure, ideally it'll eventually be fixed in the standard, but this isn't a battle that should be fought on the backs of the users/developers who just want to use good tooling (e.g. uv
) without losing useful features in their IDE. In fact I posted a comment in the linked issue (which I found after commenting in here) which, while probably not a perfect solution, provides a decent workaround that could easily be adapted to work without executing any external code.
from pylance-release.
Pylance is built on top of the pyright type checker. Pyright is a standards-compliant type checker.
The way to address this issue is to fix the Python standards so PEP 660 is compatible with static analysis tools and vice versa. The pylance issue tracker is not the right place to discuss this, so you're unlikely to make any headway here.
If you would like to propose a standard way to make this work across the ecosystem (all packaging tools and static analysis tools), you can start a discussion in the Python forums. This issue spans both typing and packaging, so you could post in either and then cross-post to the other. This will ultimately require a PEP, but a public discussion in the appropriate forum is a good place to start.
from pylance-release.
Fair enough, I just commented on what I believe is the corresponding issue on the pyright repo: microsoft/pyright#3880 (comment)
And sure, coming up with a proper solution backed by a PEP is clearly the desired long-term solution. But why not be pragmatic and have a way that fixes an obvious problem pretty quickly, instead of waiting for a process that's certainly going to last many months if not over a year...
from pylance-release.
Short-term hacks that work in some cases and for some combinations of tools is not the right answer here. Any solution to this problem will require a well-defined and sound design. If you think you have such a proposal and others in the community agree with your approach, you should be able to get to a draft PEP pretty quickly. Once there's a draft PEP that looks like it's likely to be accepted, I can implement provisional support for it in pyright.
from pylance-release.
Related Issues (20)
- "Not a known attribute" error HOT 12
- pylance crashing all the time HOT 1
- Pylance Performance Issue HOT 1
- pylance crash: Allocation failed - JavaScript heap out of memory HOT 18
- report HOT 2
- issue HOT 1
- Add all missing imports for file HOT 1
- "Go to declaration": it would be good to have suggestions HOT 1
- provide standalone file mode HOT 4
- "Go to symbol in workspace" does not see the symbol HOT 1
- Pylance is analyzing something forever (profile included) HOT 3
- What is the version of pylance corresponding to vscode version 1.80.2? HOT 2
- cyclic symbolic detection is broken
- I end up configuring this more than actually optimizing my code like pycharm provides HOT 2
- Cannot import keras.models in Pylance even though it's reexported in the `__init__.py` HOT 4
- Find all references finds references in 3rd party libraries
- Go to definition when already on a definition of a variable jumps to the first use
- Renames are not being applied to notebook cells HOT 1
- Docstring templating HOT 1
- "Add...to extraPaths" doesn't work in multiroot workspace HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pylance-release.