-
Notifications
You must be signed in to change notification settings - Fork 33
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
import resolution for generic virtual environments #365
Comments
in my experience virtualenvs just work by default in pyright, so i've left the could you give a bit more info about how exactly you're running basedpyright? are you installing it outside of your venv and if so, how come? |
Thank you for the prompt reply!
I have installed it globally as python library via |
it's not a requirement, but it's what i've always done and i don't really understand why you wouldn't want to do that. making basedpyright a dev dependency of your project allows you to pin the version used by your IDE and your CI, which will prevent new errors or other changes in behavior from randomly appearing unexpectedly. this was my main motivation for creating basedpyright in the first place. i discuss the issue in more detail here. as i said there, i recognize many people only use (based)pyright for its language server features and completely disable its type checking. i guess i could see why you'd want to just have it installed globally in that case? but i would highly recommend enabling the type checking and installing it to your venv instead. all of that said, one of my goals is to maintain backwards compatibility with pyright so that users who don't agree with all of our decisions can simply disable the features they don't like. so this
is the issue that poetry generates a random folder name for each of its venvs? if so, i think the solution here is to use an in-project venv, then set i also want to take this moment to shill pdm as an alternative to poetry. in my experience it's far more stable and most importantly, unlike poetry, the maintainers didn't make the install script randomly fail in the CI 5% of the time. (even though this change was reverted before it was released, this shows that the maintainers are not trustworthy) |
because doing so runs into the problem indicated here, namely the text editors (neovim in this case, but it would be the same for others) look for the general executable into
This unfortunately doesn't work and hasn't worked in pyright either unless one explicitly fully qualifies the precise path to the disposable virtual environment, which again defies a little the purpose of configuration per sé.
Sure, but this issue isn't exclusive to poetry, the server fails to recognise virtual environments created by other dependency managers too: Generally speaking my question can be re-formulated as follows: is it possible to have a minimally reproducible example (say in the docs or README) of how to have basedpyright working with general virtual environments? I know the docs address to the P. S. Please don't get me wrong, this project is great and I appreciate the immense effort in its development! The handling of virtual environments however has kept many away from pyright originally already (most other language servers do recognise the virtual environments automatically), I have been using others successfully so far but wanted to switch to basedpyright given the great improvements it's bringing - which is why I am still a little puzzled that virtual environments being such a basic and common python development feature, one still needs to go to great lengths to have the lsp to work with them "out of the box". |
oh wow, that's really stupid. we should definitely fix that |
It's worth calling out that while you make good points here, most editors don't care. Neovim (mason specifically) for example downloads a single LSP and uses it for all matching filetypes. Vscode will do the same. While it is nice to have your LSP as part of your dev requirements, I would hesitate to consider it a requirement to get virtualenvs functional. |
the basedpyright vscode extension defaults to using the version installed in the current project's venv, and will only fallback to using the version bundled with the extension if you don't have it installed already. i'm not sure how the extensions for other IDEs work since they were contributed by others and i personally only use vscode, but ideally they would all behave the same way.
i agree. as i said in my previous comment, it seems really dumb that the |
my experience with terminal text editors is that they default to the global LSP executable because by definition their configurations are global, whereas IDEs tend to have mechanisms to "automatically activate" virtual environments within themselves (although even here I am not sure that this activation is automatic and must not be explicitly invoked by the user instead - I remember using pyright with PyCharm and somehow I could never make it work). Generally speaking I would say that if one envisions for this project to be adopted in industrial settings (where many people contribute to the same code) then there ought to be an easi(er) way to indicate that the LSP must look for references and packages into a virtual environment first (if present) - the rationale being that if I use it myself only, then I could configure full paths and all the rest on my local machine, but if I work with my team on a shared github repository then we cannot just commit into the |
I find what has worked best for me in neovim is not the venv/venvPath configuration but to set pythonPath. Here's how you set it. In lspconfig settings there is a callback called on_new_config = function(new_config, new_root_dir)
local success, py_path = pcall(function()
local pysupport = require("python_support")
return pysupport.get_local_python_location(new_root_dir)
end)
if success and py_path then
new_config.settings.python.pythonPath = py_path
end
if not success then
vim.notify("Error: " .. tostring(py_path), vim.log.levels.WARN)
end
end, I have a function there I don't really know what I'm doing, but maybe there could be a space somewhere in this repo where we can share some documentation bits about neovim. It's a bit tricky to find out exactly what the configuration items are called inside that settings namespace. |
yeah it seems difficult to find information on how to configure neovim extensions in general, so i'm open to suggestions/contributions to improve the docs. feel free to contribute some more information about neovim either in the setup instructions (if it's something you believe all users should do) or the language server settings examples (if it's something only some users may want). the source for the docs is located here |
what are
in this context? Are they python in-built utilities in neovim or are they defined elsewhere? |
@gennaro-tedesco What I mean here is that it's something you have to fill in. I have one function there that only handles uv projects (call I think it would help if I could share the code, but even so it doesn't support poetry projects.. |
Sure, but having to manually fill in the paths (be they |
I mean fill in the function that finds the right path, not fill in the path though. Just sharing my solution so far - which calls |
revisiting this issue again now that i'm more familiar with how these settings work.
[tool.basedpyright]
venvPath = "."
venv = ".venv" |
@DetachHead Thank you for checking up again on this issue: I will test your suggestion with different virtual environment and python package managers in the next days (as soon as I get some time)! |
I confirm that with these settings [tool.basedpyright]
venvPath = "."
venv = ".venv" the warnings disappear. I have moreover tested it with virtual environments in non-standard paths and as long as the concatenation of This issue can be closed as far as I am concerned :). If however I may make a suggestion, I would recommend to create a clear minimal working example in the documentation. The point seems to be - as we have concluded - that Maybe you could wait for other users to validate this too (in case they use other environments, hardware, versions and so forth). |
yeah i agree that it's confusing. i still don't get why i will leave this issue open for now until the documentation is updated. perhaps in this section |
fixes DetachHead#365 If project root is found, and ".venv" is found under project root, use that venv as default. Project root is found if "pyrightconfig.json" or "pyproject.toml" is found. ".venv" can change to another name by change the '[tool.pyright] venv="venv_name"' in "pyproject.toml". But note that `uv` only support ".venv" so better stick with that.
thinking about this more, i'm considering just discouraging the the pyright docs seem to already recommend against using these settings but i think the documentation can be more clear about that. |
What does this solution actually look like? From the comment on the thread the suggestion is for the user to find out by themselves (using I am not against recommending |
Yes, I agree, that seems a good solution indeed. |
First of all, thank you for the awesome project!
I am trying to come to terms with resolution of package imports within virtual environments (which I often found difficult in
pyright
as well): in particular I am referring to the import resolution section in the docs. Notice I have read #158 and #214 as well.Description
I am using poetry to manage virtual environments: this means that virtual environments are generally created in some local paths
~/Library/pypoetry/virtualenvs/<projectname>-abc124...
(or similar), with python packages locally installed are referred therein.While running the project within the activated virtual environment, basedpyright always shows the diagnostic "Import could not be resolved" despite explicitly indicating the aforementioned path as per the documentation:
The documentation moreover mentions
which is a little unclear to me if you are not using VS Code (the
pythonPath
is not a variable that even exists or is/must be explicitly set).Attempts
What is the recommended way to specify paths to virtual environments for package resolution? (Notice I am referring to package imports and not relative imports from other modules, which work fine instead).
The text was updated successfully, but these errors were encountered: