17

I can't figure out why pyenv (installed via homebrew) doesn't seem to work.

It seems like my $PATH variable isn't updated correctly by pyenv and therefore none of the interpreters installed via pyenv can be found. For example, for python version 3.6.8:

$ pyenv versions
  system
* 3.4.10 (set by /Users/cglacet/.pyenv/version)
* 3.5.7 (set by /Users/cglacet/.pyenv/version)
* 3.6.8 (set by /Users/cglacet/.pyenv/version)
* 3.7.3 (set by /Users/cglacet/.pyenv/version)
* 3.8-dev (set by /Users/cglacet/.pyenv/version)

$ pyenv which python3.6
/Users/cglacet/.pyenv/versions/3.6.8/bin/python3.6

$ $(pyenv which python3.6) --version
Python 3.6.8

$ pyenv shell
pyenv: no shell-specific version configured

$ pyenv local
pyenv: no local version configured for this directory

Up until here everything looks just fine, but:

$ python3.6 --version
-bash: python3.6: command not found

$ python --version
Python 3.7.0

If I check my PATH environment variable, I can't see any path of the form /Users/cglacet/.pyenv/versions/3.x.x/bin.

Note that 3.7.0 is the python version I had before installing pyenv (the system one). What I expect is to have 3.6 available (all versions installed via pyenv), which should be the case as I activated it as a global interpreter as shown before. The expected behavior is:

$ python3.6 --version
Python 3.6.8
cglacet
  • 8,873
  • 4
  • 45
  • 60
  • What is the expected functionality? Please add it. – Mithilesh_Kunal Jun 05 '19 at 15:52
  • I struggled doing this on my phone, but that’s done. I simply wish to have the executable “python3.6” available in my path. – cglacet Jun 05 '19 at 16:04
  • What do you get when you run `which pyenv` and `which python`? – Code-Apprentice Jun 05 '19 at 16:08
  • Not sure but I think /usr/local/bin – cglacet Jun 05 '19 at 16:30
  • 1
    There is no indication that you executed `pyenv init` or otherwise set it up according to [instructions](https://github.com/pyenv/pyenv#basic-github-checkout). We can't tell you what's wrong with your installation because you haven't provided an MCVE. "I'll update the title as soon as I'll have a vague idea of what is going on." SO is not a personal help site. It is a documentation site. This is something you need to complete before you post. – jpmc26 Jun 05 '19 at 16:33
  • Ah no, python is in /usr/local/opt/libexec/bin/python – cglacet Jun 05 '19 at 16:35
  • If I had any idea of the problem I would solve it myself... I don’t get your point, once the reason of this happening will be clear, I’ll update the title and so others may have a chance of finding it if they have a similar issue – cglacet Jun 05 '19 at 16:38
  • 1
    @cglacet It is highly unlikely that you are going to be able to capture all the possible causes of this error in a single question. Any number of changes to the host system or mistakes made during installation could cause it. [There is not enough information here for a person trying to answer the question to determine if their belief about the cause is correct.](https://meta.stackoverflow.com/a/385640/1394393) My point is that this is not a good question because of that. – jpmc26 Jun 05 '19 at 17:04
  • I found a fix, but I still have no idea what happened, reading the instructions again I understand that there must be some kind of place (.bashrc?) where pyenv adds stuff. I had to modify my path and add `shims` manually in my bash profile. – cglacet Jun 05 '19 at 17:06
  • @cglacet Please use the `@` syntax to reply; otherwise, the other party is not notified. It sounds like you're not familiar with how Linux systems work. bashrc is a script that gets automatically executed when a user launches a new instance of bash. .bash_profile and .profile are *similar* but not exactly the same. (They're all invoked under different conditions.) There is no global registry of environment variables like in Windows. Look into how to make changes to environment variables permanent for more details. – jpmc26 Jun 05 '19 at 17:14
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194514/discussion-between-cglacet-and-jpmc26). – cglacet Jun 05 '19 at 21:24

2 Answers2

33

After a bit of digging I found that homebrew install failed to edit my .bash_profile. The problem is that pyenv itself doesn't rely on these additions and therefore the bug is silent (you just don't have the interpreters in your path).

If you are in this case you'll have to run part of the install manually (starting at "#2 Configure your shell's environment for Pyenv" and add the following in your ~/.bash_profile (preferably append this new path so it arrives before your system python path, in other word, append this at the end of your bash profile):

export PATH=$(pyenv root)/shims:$PATH

That solves the problem I had (as the directory $(pyenv root)/shims contains all the interpreters you installed via pyenv). But you might want to have the complete set of features that pyenv offers (eg., autocompletion of commands), which (in theory) could be done by adding the following to your .bash_profile instead of the PATH export:

eval "$(pyenv init -)"

But for me that didn't work as pyenv init produced some faulty code (missing function declaration), on the other hand you can use the following and it should work (better):

eval "$(pyenv init - | sed 's:^pyenv() :function pyenv():')"

I still have no idea why the installation failed on my system, if anyone as a clue that would be interesting (and that would probably deserve a fix because I probably won't be the only one having this issue).

cglacet
  • 8,873
  • 4
  • 45
  • 60
  • Thanks! Same problem! my python version on env was pointing to local python in machine and with one line of I was able to solve it. – Reihan_amn Nov 20 '19 at 02:01
  • Are you on osx too? – cglacet Nov 20 '19 at 14:36
  • @cglacet I am on OSX 10.15.4 and same issue. Thanks – WebOrCode May 22 '20 at 09:26
  • Thanks, this helped a lot! For fish shell add "set PATH (eval pyenv root)/shims $PATH" to config.fish – Andreas Kraft Nov 29 '21 at 13:16
  • This doesn't fix the path for items in .pyenv/versions/3.6.8/bin/ though. It just fixes it for the main shims (pip python python3 etc). I assume there is a similar command to return the path to the currently selected version, similar to (pyenv root) so adding $(pyenv version?)/bin or whatever to the path as well EDIT: running "pyenv rehash" fixes that issue by creating shims for everything in the versions/3.x/bin folder. It's supposed to automatically run it when you install a package through the shim, but that doesn't appear to be working properly – JReader Dec 22 '21 at 01:00
1

pyenv has decopuled the path prepending into a separate init command. In your dotfiles you want an additional pyenv init --path:

eval "$(pyenv init -)"
eval "$(pyenv init --path)"

If you run this command manually (without evaluating it) examine what it does:

$ pyenv init --path
export PATH="/Users/yourname/.pyenv/shims:${PATH}"
Gabe Martin-Dempesy
  • 7,687
  • 4
  • 33
  • 24