Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 209
hpy Avatar asked May 10 '21 16:05

hpy


Video Answer


2 Answers

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
like image 120
Yogesh Yadav Avatar answered Sep 28 '22 11:09

Yogesh Yadav


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)

like image 44
faho Avatar answered Sep 28 '22 11:09

faho