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

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

Closed
dan-blanchard opened this issue Mar 23, 2018 · 11 comments · Fixed by #23094
Closed

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

dan-blanchard opened this issue Mar 23, 2018 · 11 comments · Fixed by #23094
Assignees
Labels
area-environments Features relating to handling interpreter environments feature-request Request for new features or functionality linux macos needs PR Ready to be worked on on-testplan Added to test plan
Milestone

Comments

@dan-blanchard
Copy link

Environment data

  • VS Code version: 1.21.1
  • Extension version (available under the Extensions sidebar): 2018.2.1
  • OS and version: macOS 10.13.3
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.6.4
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): pyenv
  • Relevant/affected Python packages and their versions: pyenv 1.2.1

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 brettcannon added feature-request Request for new features or functionality needs PR labels Mar 26, 2018
@brettcannon
Copy link
Member

brettcannon commented Mar 26, 2018

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
Copy link

neverfox commented Apr 2, 2018

@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.

@brettcannon brettcannon added this to the May 2018 milestone Apr 11, 2018
@brettcannon brettcannon changed the title Does not select pyenv environment based on folder .python-version file Select pyenv environment based on folder .python-version file Apr 24, 2018
@brettcannon brettcannon modified the milestones: May 2018, June 2018 May 7, 2018
@brettcannon brettcannon removed this from the June 2018 milestone Jun 4, 2018
@gramster gramster added area-environments Features relating to handling interpreter environments and removed area-environments Features relating to handling interpreter environments feature-interpreter labels Oct 10, 2019
@eugenweissbart
Copy link

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

@brettcannon
Copy link
Member

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

@djdembeck
Copy link

Would like to see this feature as well.

@brettcannon
Copy link
Member

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

@Walker555

This comment was marked as duplicate.

@karrtikr
Copy link

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 onDidExecuteTerminalCommand does not fire events for hidden terminal vscode#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 microsoft/vscode#199611 is fixed.

@karrtikr
Copy link

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

image

@karrtikr karrtikr added the on-testplan Added to test plan label Mar 22, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-environments Features relating to handling interpreter environments feature-request Request for new features or functionality linux macos needs PR Ready to be worked on on-testplan Added to test plan
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants