pyenv在激活虚拟环境时不再正确设置路径



我已经使用pyenv将近两年了,在X11模式下运行RHEL 8.3(Linux内核4.18(和Gnome 3.32.2的系统上没有任何问题。我主要使用鱼壳,但偶尔也会使用bash,直到现在,这两种方法都能很好地使用pyenv。但是,在大约24小时前运行pyenv update之后,使用pyenv activate命令激活我创建的一个虚拟环境将不再设置使用我在该虚拟环境中安装的内容的路径。

当我启动终端会话时,我看到一条新消息,上面写着:

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

所以我运行了pyenv init,它告诉我:

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

我确信我已经拥有了以上所有的东西。这是我的~/.profile:

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

这是我的~/.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

我的~/.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 -)"

更多观察:

  1. 我发现,尽管我有~/.profile,但当我登录到桌面环境时,它从未被来源/运行
  2. set -Ux PYENV_ROOT $HOME/.pyenvset -Ux fish_user_paths $PYENV_ROOT/bin $fish_user_paths放在~/.profile~/.config/fish/config.fish中没有区别

最后,即使在激活了pyenv创建的虚拟环境后,我仍然无法访问其中的内容。

如何解决此问题?非常感谢。

OS:ubuntu 18.04

我通过更新了pyenv

pyenv update 

并且它开始显示这个警告

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

CCD_ 14不再正确设置路径

修复

我的~/.bashrc中有这些行。因此,删除/注释如果您在~/.bashrc中有这些行

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

在采购~/.bashrc之前,将这些行添加到~/.profile

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

源配置文件

source ~/.profile

消息错误。~/。profile是针对posix兼容的shell的,而fish不是,所以它不会读取它。这应该作为pyenv中的一个bug报告。

现在,您已经在config.fish中有了要添加到$PATH中的代码,因此似乎没有必要从您的端进行进一步的操作,该警告不适用于您。

你的~/.配置文件是fish代码,它与将读取它的shell不兼容,我建议删除它。


如果您还没有所有必要的位:

奇怪的是,实际的代码与fish兼容(除了pyenv init --path,它打印posix代码。我建议跳过它(。由于它建议通过set -U使用通用变量,并且这些变量是持久的,因此您只需要以交互方式运行set -U一次:

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

您确实不想在config.fish中添加这些。fish保留通用变量,而这一个添加到$fish_user_path中,因此每次打开一个fish时,它都会添加一个组件,这只会让您在其中留下数百个元素,并减慢fish的速度。

或者,在config.fish中将$PYENV_ROOT/bin添加到$PATH中,就像您已经在做的$PATH添加一样:

# 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

(或使用fish_add_path,在鱼3.2-fish_add_path /home/[username]/.local/share/junest/bin $PYENV_ROOT/bin中装运-这将注意不要多次添加(

保留pyenv init - | source(最终与source (pyenv init -|psub)相同,没有无用的临时文件,因此它是首选(

只需将~/.bashrc文件中的eval "$(pyenv init -)"行替换为eval "$(pyenv init --path)"即可。

请参阅此问题:https://github.com/pyenv/pyenv/issues/1906

对于Mac和fish,将其添加到~/.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

对于那些已经解决了这个问题并且正在使用鱼以外的东西的人,我也尝试遵循pyenv -init给出的指示,但没有成功。像@jmunsch一样,我有Ubuntu 20.04和bash。然而,在pyenv -init运行之前,~/.profile似乎没有被读取,或者我的bash配置中的某些内容正在覆盖相关的~/.profile设置。不知道这可能是什么,但在有人问之前:

  1. 是的,~/.profile更改设置在来源~/.bashrc的行之前
  2. 不,我不使用~/.bash_profile~/.bash_login

以下是我在~/.bashrc:中所做的ALL

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

我还没有彻底测试pyenv,但警告已经消失,它似乎运行正常。

最新更新