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

Question:

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?

Asked By: Robino

||

Answers:

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 😉

Answered By: Bas Krahmer

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
Answered By: tomy0000000
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.