hpy
hpy

Reputation: 2161

pyenv no longer sets paths correctly when activating virtual environments

I've been using pyenv for almost two years with no problems on my system running RHEL 8.3 (Linux kernel 4.18) with Gnome 3.32.2 in X11 mode. I primarily use the fish shell but also occasionally bash, both worked fine with pyenv until now. However, after running pyenv update about 24 hours ago, using the pyenv activate command to activate one of my created virtual environments no longer sets the path to use what I've installed in that virtual environment.

When I start a terminal session, I see a new message saying:

WARNING: `pyenv init -` no longer sets PATH.
Run `pyenv init` to see the necessary changes to make to your configuration.

So I ran pyenv init which told me:

# Add pyenv executable to PATH by adding
# the following to ~/.profile:

set -Ux PYENV_ROOT $HOME/.pyenv
set -Ux fish_user_paths $PYENV_ROOT/bin $fish_user_paths

# Load pyenv automatically by appending
# the following to ~/.config/fish/config.fish:

pyenv init - | source

# and the following to ~/.profile:

pyenv init --path | source

# If your ~/.profile sources ~/.config/fish/config.fish,
# the lines should be inserted before the part
# that does that.

# Make sure to restart your entire logon session
# for changes to ~/.profile to take effect.

I'm pretty sure I already have all of the above. Here's my ~/.profile:

set -Ux PYENV_ROOT $HOME/.pyenv
set -Ux fish_user_paths $PYENV_ROOT/bin $fish_user_paths
pyenv init --path | source

Here's my ~/.config/fish/config.fish:

# Set default editor
set -gx EDITOR nano

# Set Junest path
set PATH /home/[username]/.local/share/junest/bin $PATH

# Set pyenv root directory
set PYENV_ROOT $HOME/.pyenv

# Add pyenv and local bin to $PATH
set PATH $PYENV_ROOT/bin /home/[username]/.local/bin $PATH

# Add pyenv init to shell to enable shims and autocompletion 
# Note the command `pyenv init` will tell you to add this line for fish
status --is-interactive; and source (pyenv init -|psub)

pyenv init - | source

My ~/.bashrc:

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific environment
PATH="$HOME/.local/bin:$HOME/bin:$PATH"
export PATH

# Uncomment the following line if you don't like systemctl's auto-paging featur$
# export SYSTEMD_PAGER=

# User specific aliases and functions

# Load pyenv
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

A few more observations:

  1. I discovered that even though I have ~/.profile, it is never sourced/run when I log into my desktop environment.
  2. Putting set -Ux PYENV_ROOT $HOME/.pyenv and set -Ux fish_user_paths $PYENV_ROOT/bin $fish_user_paths in ~/.profile or ~/.config/fish/config.fish doesn't make a difference.

In the end, even after activating a pyenv-created virtual environment, I still don't have access to what's in it.

How can I troubleshoot this? Thank you.

Upvotes: 21

Views: 15027

Answers (5)

Yogesh Yadav
Yogesh Yadav

Reputation: 4745

OS: ubuntu 18.04

I updated pyenv by

pyenv update 

and it start showing this warning

WARNING: `pyenv init -` no longer sets PATH.
Run `pyenv init` to see the necessary changes to make to your configuration.

and pyenv no longer sets the path correctly

FIX

I have these lines in my ~/.bashrc . So, delete/comment these lines if you have in ~/.bashrc

export PATH="/home/yogi/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Add these lines in ~/.profile before sourcing ~/.bashrc

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"

Source profile

source ~/.profile

Upvotes: 17

Shmidt
Shmidt

Reputation: 16664

For Mac and fish, add this to ~/.config/fish/config.fish:

set PYENV_ROOT $HOME/.pyenv
set -x PATH $PYENV_ROOT/shims $PATH
set -x PATH $PYENV_ROOT/bin $PATH

if command -v pyenv 1>/dev/null 2>&1
    pyenv init - | source
end

Upvotes: 5

Gunjan
Gunjan

Reputation: 1244

Just replace the line eval "$(pyenv init -)" from your ~/.bashrc file to eval "$(pyenv init --path)".

Please refer this issue: https://github.com/pyenv/pyenv/issues/1906

Upvotes: 6

Flic
Flic

Reputation: 751

For those who have landed on this question and are using something other than fish, I also tried to follow the directions given by pyenv -init without success. Like @jmunsch, I have Ubuntu 20.04 and bash. However, it appears ~/.profile is either not being read or something in my bash configuration is overwriting the relevant ~/.profile settings before pyenv -init gets to run. No idea what this may be, but before anyone asks:

  1. Yes, the ~/.profile changes were set before the line that sources ~/.bashrc
  2. No, I don't use ~/.bash_profile or ~/.bash_login

Here's what I did instead, ALL in ~/.bashrc:

export PYENV_ROOT=$HOME/.pyenv
export PATH=$PYENV_ROOT/bin:$PATH
# <Other commands not relevant to pyenv - jump straight to evals if you like>
# .
# .
eval "$(pyenv init --path)"
eval "$(pyenv init -)"

I haven't tested pyenv exhaustively, but the warning has gone away and it seems to be functioning OK.

Upvotes: 2

faho
faho

Reputation: 15924

The message is wrong. ~/.profile is for posix-compatible shells, which fish is not, and so it won't read it. This should be reported as a bug in pyenv.

Now, you already have the code to add to $PATH in your config.fish, so it appears no further action from your end is necessary, the warning does not apply to you.

Your ~/.profile is fish-code, which is incompatible with shells that will read it and I suggest removing it.


If you didn't already have all the necessary bits:

Weirdly enough the actual code is fish-compatible (except for pyenv init --path, which prints posix code. I suggest just skipping it). Since it recommends using universal variables via set -U, and those persist, you'll just want to run the set -U once interactively:

set -Ux PYENV_ROOT $HOME/.pyenv
set -Ux fish_user_paths $PYENV_ROOT/bin $fish_user_paths

You do not want to add these in config.fish. Fish persists universal variables and this one adds to $fish_user_paths, so it will add one component every time you open a fish, which will just end up leaving you with hundreds of elements in it and slow down fish.

Alternatively add $PYENV_ROOT/bin to $PATH yourself in config.fish, just like the $PATH additions you're already doing:

# Set pyenv root directory
set -gx PYENV_ROOT $HOME/.pyenv
# (note that this must now come later because it uses $PYENV_ROOT)
set -gx PATH /home/[username]/.local/share/junest/bin $PYENV_ROOT/bin $PATH

(or use fish_add_path, shipped in fish 3.2 - fish_add_path /home/[username]/.local/share/junest/bin $PYENV_ROOT/bin - this will take care of not adding it multiple times)

Keep the pyenv init - | source (which ends up doing the same as source (pyenv init -|psub) without a useless temp file, so it's preferred)

Upvotes: 7

Related Questions