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

hpy*_*hpy 13 python fish pyenv pyenv-virtualenv

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

当我开始终端会话时,我看到一条新消息:

WARNING: `pyenv init -` no longer sets PATH.
Run `pyenv init` to see the necessary changes to make to your configuration.
Run Code Online (Sandbox Code Playgroud)

所以我跑了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.
Run Code Online (Sandbox Code Playgroud)

我很确定我已经拥有了上述所有内容。这是我的~/.profile

set -Ux PYENV_ROOT $HOME/.pyenv
set -Ux fish_user_paths $PYENV_ROOT/bin $fish_user_paths
pyenv init --path | source
Run Code Online (Sandbox Code Playgroud)

这是我的~/.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
Run Code Online (Sandbox Code Playgroud)

我的~/.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 -)"
Run Code Online (Sandbox Code Playgroud)

还有一些观察:

  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 创建的虚拟环境,我仍然无法访问其中的内容。

我该如何解决这个问题?谢谢你。

Yog*_*dav 8

操作系统: ubuntu 18.04

我更新pyenv

pyenv update 
Run Code Online (Sandbox Code Playgroud)

它开始显示此警告

WARNING: `pyenv init -` no longer sets PATH.
Run `pyenv init` to see the necessary changes to make to your configuration.
Run Code Online (Sandbox Code Playgroud)

并且pyenv不再正确设置路径

使固定

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

export PATH="/home/yogi/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
Run Code Online (Sandbox Code Playgroud)

采购之前添加这些行~/.bashrc

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
Run Code Online (Sandbox Code Playgroud)

源配置文件

source ~/.profile
Run Code Online (Sandbox Code Playgroud)

  • 还修复了 MacOS 上的问题 (2认同)
  • 刚刚在 Ubuntu 20.04 上为我工作,谢谢。 (2认同)
  • 这在 WSL2 上也对我有用!多谢。 (2认同)

fah*_*aho 6

消息是错误的。~/.profile 用于兼容 posix 的 shell,而 fish 不是,因此它不会读取它。这应该报告为 pyenv 中的错误。

现在,您已经将代码添加到 config.fish 中的 $PATH 中,因此您似乎不需要采取进一步的行动,警告不适用于您。

您的 ~/.profile 是鱼代码,它与会读取它的 shell 不兼容,我建议将其删除。


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

奇怪的是,实际的代码是兼容鱼的(除了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
Run Code Online (Sandbox Code Playgroud)

希望在config.fish添加这些。Fish 保留了通用变量,而这个变量会添加到 $fish_user_paths 中,因此每次您打开一条鱼时它都会添加一个组件,这最终只会给您留下数百个元素并减慢鱼的速度。

或者将 $PYENV_ROOT/bin 自己添加到 config.fish 中的 $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
Run Code Online (Sandbox Code Playgroud)

(或使用fish_add_path,在 fish 3.2 中发货 - fish_add_path /home/[username]/.local/share/junest/bin $PYENV_ROOT/bin- 这将不会多次添加它)

保持pyenv init - | source(最终与source (pyenv init -|psub)没有无用的临时文件一样,所以它是首选)


Gun*_*jan 6

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

请参考这个问题:https ://github.com/pyenv/pyenv/issues/1906


Shm*_*idt 5

对于 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
Run Code Online (Sandbox Code Playgroud)