How do I get pyenv to display the executable path for an installed version?
Asked Answered
C

4

13

Install a python version using:

$ pyenv install 3.8.9
Installed Python-3.8.9 to /Users/robino/.pyenv/versions/3.8.9

List the python versions now available:

$ pyenv versions
  * system
  3.8.2
  3.8.9

A week goes by and I forget where it is installed. Now suppose I want to get the executable path for 3.8.9 version. The following do not work:

$ pyenv which 3.8.9
  pyenv: 3.8.9: command not found
$ pyenv which python 3.8.9
  (gives path to system python)
$ pyenv which python-3.8.9
  pyenv: python-3.8.9: command not found
$ pyenv which Python-3.8.9
  pyenv: Python-3.8.9: command not found

A workaround I found was to set the python version, check, then set it back to system:

$ pyenv local 3.8.9
$ pyenv which python
  /Users/robino/.pyenv/versions/3.8.9/bin/python
$ pyenv local --unset

However this is a suboptimal solution as it required that no local is previous set.

What is the correct command to print out the python executable path for a currently not used version, using pyenv?

Comparable answered 29/9, 2022 at 16:32 Comment(0)
L
18

By default, pyenv executable can be found at $(pyenv root)/versions/{VERSION}/bin/python. I am not aware of a command displaying all/any executables other than pyenv which python.

If you'd like to get the path via commands though, another option would be to make a temporary subdirectory and set the local pyenv interpreter there:

$ mkdir tmp; cd tmp
$ pyenv local 3.8.9
$ pyenv which python
  /Users/robino/.pyenv/versions/3.8.9/bin/python
$ cd ..; rm -r tmp

Since deeper directories take priority with local pyenv versions, a parent directory wouldn't interfere in this case.

Yet another option would be to temporarily set the global pyenv version, as this does not have the requirement of no local pyenv version being set. I wouldn't like this though as I'd probably forget to set it back to its original value ;)

Lelialelith answered 1/10, 2022 at 12:44 Comment(1)
I had thought of the deeper directory, but what a hack! Seems like you can only get the path for the currently deployed executable, which is a shame.Comparable
C
14

I simplified it a little, and the command runs in a subcommand, should be immune to side effect.

echo $(pyenv shell 3.9.15; pyenv which python)
# /Users/tomy0000000/.pyenv/versions/3.9.15/bin/python
Cottonmouth answered 11/12, 2022 at 13:52 Comment(2)
I am getting "pyenv: shell integration not enabled. Run `pyenv init' for instructions."Comparable
@Comparable It's likely that you did not configure your shell config for pyenv, check thisCottonmouth
S
6

You can combine Bas Krahmer's answer with the fact that setting the environment variable PYENV_VERSION determines the version used by pyenv:

$ PYENV_VERSION=3.11.4 pyenv which python
/home/my_username/.pyenv/versions/3.11.4/bin/python
Spy answered 28/8, 2023 at 8:22 Comment(0)
P
0
  1. Run pyenv init and add the output to your shell configuration file.
  2. Restart shell.
Portwin answered 19/8 at 11:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.