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

Nil*_*esh 6 python bash sudo pyenv

我正在按照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 -)"
Run Code Online (Sandbox Code Playgroud)

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

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

$ sudo pyenv
sudo: pyenv: command not found
Run Code Online (Sandbox Code Playgroud)

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

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

我也将该.bash_profile内容移至.bashrcof/root/但仍然给出相同的错误。

怎么说呢sudo,运行的时候请先加载.bashrc还是.bash_profile先。

小智 9

在此 git 讨论中找到的解决方案:

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

sudo env "PATH=$PATH VIRTUAL_ENV=$VIRTUAL_ENV" python myscript.py
Run Code Online (Sandbox Code Playgroud)