首页 > 解决方案 > Pyenv 用作 sudo 时不显示所有版本

问题描述

我使用 pyenv安装3.5.2和版本。3.5.3

# pyenv versions
* system (set by /usr/local/pyenv/version)
  3.5.2
  3.5.3

但是,当我以sudo(而不是以登录身份root)运行此命令时,它并没有给我所有版本。

$ sudo /usr/local/bin/pyenv versions
* system (set by /root/.pyenv/version)

我尝试使用设置PYENV_ROOT路径,但这也不起作用。

$ export PYENV_ROOT=/usr/local/pyenv/
$ sudo /usr/local/pyenv/bin/pyenv versions
* system (set by /root/.pyenv/version)

我已经.bash_profile在 myuser中设置了路径

$ cat ~/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
export PYENV_ROOT=/usr/local/pyenv/
export PATH="/usr/local/pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

也在root用户中设置

$ sudo cat /root/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
export PYENV_ROOT=/usr/local/pyenv/
export PATH="/usr/local/pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

我正在使用centos

$ cat /etc/issue
CentOS release 6.9 (Final)

标签: pythoncentospyenv

解决方案


$ export PYENV_ROOT=/usr/local/pyenv/
$ sudo /usr/local/pyenv/bin/pyenv versions

这不起作用,因为PYENV_ROOT不会传递给 sudo 中的环境。尝试这个:

$ sudo PYENV_ROOT=/usr/local/pyenv/ /usr/local/pyenv/bin/pyenv versions

或这个:

$ export PYENV_ROOT=/usr/local/pyenv/
$ sudo -E /usr/local/pyenv/bin/pyenv versions

-E将使环境变量传递给 pyenv。在手册页中sudo

 -E, --preserve-env
             Indicates to the security policy that the user wishes to preserve their existing environment variables.  The security policy may return an error if the user does not have permission to
             preserve the environment.

 --preserve-env=list
             Indicates to the security policy that the user wishes to add the comma-separated list of environment variables to those preserved from the user's environment.  The security policy may
             return an error if the user does not have permission to preserve the environment.

.bash_profilein root 不起作用,因为在sudo这种情况下不会加载它。如果您更喜欢将配置写入.bash_profile.


推荐阅读