Pyenv在用作sudo时不显示所有版本

时间:2018-05-25 17:40:34

标签: python centos pyenv

我使用pyenv安装了3.5.23.5.3版本。

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

但是,当我以sudo运行此命令时(不以[{1}}登录),它不会给我所有版本。

root

我尝试使用设置$ sudo /usr/local/bin/pyenv versions * system (set by /root/.pyenv/version) 路径,但这也行不通。

PYENV_ROOT

我已在myuser

中的$ export PYENV_ROOT=/usr/local/pyenv/ $ sudo /usr/local/pyenv/bin/pyenv versions * system (set by /root/.pyenv/version) 中设置了路径
.bash_profile

同时在$ 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

我正在使用centos

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

1 个答案:

答案 0 :(得分:1)

$ 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_profile不起作用,因为在这种情况下sudo不会加载它。如果您希望在.bash_profile中编写配置,则可以参考this