Interpreters installed via pyenv are not added to $PATH
Asked Answered
R

2

22

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
Refusal answered 5/6, 2019 at 15:48 Comment(11)
What is the expected functionality? Please add it.Bypath
I struggled doing this on my phone, but that’s done. I simply wish to have the executable “python3.6” available in my path.Refusal
What do you get when you run which pyenv and which python?Budworth
Not sure but I think /usr/local/binRefusal
There is no indication that you executed pyenv init or otherwise set it up according to instructions. 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.Evade
Ah no, python is in /usr/local/opt/libexec/bin/pythonRefusal
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 issueRefusal
@Refusal 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. My point is that this is not a good question because of that.Evade
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.Refusal
@Refusal 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.Evade
Let us continue this discussion in chat.Refusal
R
35

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

Refusal answered 5/6, 2019 at 22:57 Comment(5)
Thanks! Same problem! my python version on env was pointing to local python in machine and with one line of <export PATH=$(pyenv root)/shims:$PATH> I was able to solve it.Snaggletooth
Are you on osx too?Refusal
@Refusal I am on OSX 10.15.4 and same issue. ThanksDashiell
Thanks, this helped a lot! For fish shell add "set PATH (eval pyenv root)/shims $PATH" to config.fishHibbler
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 properlyPacking
V
4

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}"
Vaginal answered 12/4, 2022 at 18:56 Comment(1)
Why is this not mentioned in the documentation? github.com/pyenv/pyenv#set-up-your-shell-environment-for-pyenvDiatropism

© 2022 - 2024 — McMap. All rights reserved.