与 sudo 一起使用时未设置 pyenv 变量

pyenv variable not set when used with sudo

我正在跟着https://amaral.northwestern.edu/resources/guides/pyenv-tutorial安装pyenv。

我安装了 pyenv 并将 .bash_profile 更新为

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

我添加了我的用户和 root,两者都可以毫无问题地访问 pyenv

当我 运行 sudo pyenv 时它给出错误。

$ sudo pyenv
sudo: pyenv: command not found

但是当我 运行 登录到该用户后,它工作正常。

$ sudo -i
# pyenv
pyenv 1.2.4
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   commands    List all available pyenv commands
   local       Set or show the local application-specific Python version
   global      Set or show the global Python version
   shell       Set or show the shell-specific Python version
   install     Install a Python version using python-build
   uninstall   Uninstall a specific Python version
   rehash      Rehash pyenv shims (run this after installing executables)
   version     Show the current Python version and its origin
   versions    List all Python versions available to pyenv
   which       Display the full path to an executable
   whence      List all Python versions that contain the given executable

See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme

我也将 .bash_profile 内容移动到 /root/.bashrc,但它仍然给出相同的错误。

怎么说sudo,当你运行时,请先加载.bashrc.bash_profile

this git discussion 中找到的解决方案:

你必须将 $USER env 传递给 sudo:

sudo env "PATH=$PATH VIRTUAL_ENV=$VIRTUAL_ENV" python myscript.py