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

时间:2021-05-10 16:05:24

标签: python fish pyenv pyenv-virtualenv

我已经使用 pyenv 近两年了,在运行 RHEL 8.3(Linux 内核 4.18)和 X11 模式下的 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 创建的虚拟环境,我仍然无法访问其中的内容。

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

6 个答案:

答案 0 :(得分:8)

操作系统: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.

pyenv 不再正确设置路径

修复

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

中有这些行,请删除/评论
export PATH="/home/yogi/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

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

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

来源简介

source ~/.profile

答案 1 :(得分: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

不想想在 config.fish 中添加这些。 Fish 保持通用变量,并且这个添加到 $fish_user_paths,所以每次你打开一条鱼时它都会添加一个组件,这最终会让你在其中留下数百个元素并减慢鱼的速度。

或者自己在 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,在fish 3.2 中发货 - fish_add_path /home/[username]/.local/share/junest/bin $PYENV_ROOT/bin - 这将确保不会多次添加)

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

答案 2 :(得分:2)

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

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

这是我在~/.bashrcALL所做的:

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 进行详尽的测试,但警告已经消失,它似乎运行正常。

答案 3 :(得分:2)

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

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

答案 4 :(得分:0)

对于 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

答案 5 :(得分:-1)

Ubuntu 20.04 / bash

  1. ~/.bashrc~/.profile

    中删除 pyenv init 的任何旧行
  2. 在 bashrc 底部添加 eval "$(pyenv init -)"

  3. eval "$(pyenv init --path) 添加到 ~/.profile BEFORE .bashrc 来源。