microsoft / vscode-python

Python extension for Visual Studio Code
https://aka.ms/pvsc-marketplace
MIT License
4.25k stars 1.15k forks source link

Select pyenv environment based on folder .python-version file #1167

Closed dan-blanchard closed 3 months ago

dan-blanchard commented 6 years ago

Environment data

Actual behavior

You have to manually select the pyenv interpreter for each workspace folder.

Expected behavior

It should detect the workspace pyenv interpreter based on the .python-version file present in the workspace folder root.

For example, every folder I have added to my workspace has a .python-version file that pyenv creates when you run pyenv local NAME_OF_VENV in that folder. The file contains a single string which is the name of the folder/symlink in ~/.pyenv/versions/ that should be used.

brettcannon commented 6 years ago

https://github.com/pyenv/pyenv#choosing-the-python-version has the specification on how pyenv chooses an interpreter by default. I think detecting the file and using it as appropriate seems like a sensible enhancement (maybe even the file traversal to the parent directory, but not as sure on that one).

neverfox commented 6 years ago

@brettcannon I'm not sure if it's possible but you should be able to let pyenv do the heavy lifting if you use the shims it provides as the interpreter, as long as its run in a context (i.e. directory) aware way.

eugenweissbart commented 4 years ago

Hi @brettcannon, will this issue be addressed in any near future? Thanks!

brettcannon commented 4 years ago

@eugenweissbart sorry, we don't give ETAs since they always turn out to be wrong. 😉

djdembeck commented 2 years ago

Would like to see this feature as well.

brettcannon commented 2 years ago

@djdembeck please make sure to upvote the opening comment as that's how we determine community interest.

eytanhanig commented 1 year ago

please make sure to upvote the opening comment as that's how we determine community interest.

@brettcannon This is now the 8th most upvoted issue in this project. Is there anything else we can do to get this prioritized?

brettcannon commented 1 year ago

This is now the 8th most upvoted issue in this project. Is there anything else we can do to get this prioritized?

Solve the first 7 issues or write a PR? 😉 Unfortunately our environment time has been spent on UX improvements that affect everyone as of late (especially beginners), so we have not tried to tackle this specific issue (or other issues related to specific tools).

We are also working on a proposed API for the extension which would allow someone to create their own extension to implement this functionality if someone wanted to.

Walker555 commented 1 year ago

Would like to see this feature as well.

karrtikr commented 3 months ago

Spike results

Couple ways to go about it:

  1. pyenv determines which Python version to use by reading it from the following sources, in this order: https://github.com/pyenv/pyenv/blob/master/README.md#understanding-python-version-selection

    Pros: No need to launch the pyenv binary

    Cons: This would imply that we do the heavy lifting:

    • Looking into parent directories
    • Reading and parsing .python-version file correctly
    • Keeping up to date with the doc to make sure the order isn't changing
  2. Run <pyenv> which python in the cwd of the workspace. We can either: 2.1 Locate the pyenv binary and run the command using child_process.exec.

    For eg. it could be /home/node/.pyenv/bin/pyenv which python.

    2.2 Open a hidden terminal and simply run the command: pyenv which python.

    2.2 is potentially more reliable as it makes sure shell initialization scripts are being run, which include pyenv install instructions. However this requires https://github.com/microsoft/vscode/issues/199611 to be fixed.

To avoid relying on internal details of pyenv and keep things simple, approach 2 sounds reasonable.

Tested 2.1 and it seems to be working just fine on Windows and Linux, despite not executing the shell instructions, hence deciding to go with that for now. If we observe issues with that, we can switch to 2.2 once https://github.com/microsoft/vscode/issues/199611 is fixed.

karrtikr commented 3 months ago

Fix should be out in the pre-release version of the extension, use the following to try it out:

image