pyenv variable not set when used with sudo
Asked Answered
C

1

6

I am following https://amaral.northwestern.edu/resources/guides/pyenv-tutorial to install pyenv.

I installed pyenv and update the .bash_profile as

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

I added in my user and root, both can access pyenv without any issue.

When I run sudo pyenv it gives error.

$ sudo pyenv
sudo: pyenv: command not found

But when I run after login to that user, it works fine.

$ 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

I also move that .bash_profile content to .bashrc of /root/ but still it gives same error.

How to say sudo that, when you run, please load .bashrc or .bash_profile first.

Commode answered 22/5, 2018 at 18:46 Comment(3)
unix.stackexchange.com/questions/228314/…Incautious
Doesn't sudo -i pyenv do what you want?Relegate
@Relegate I am trying to do this using ansible let me see how can I pass sudo -i in ansible command module.Commode
B
9

Solution found in this git discussion:

You have to pass $USER env to sudo:

sudo env "PATH=$PATH VIRTUAL_ENV=$VIRTUAL_ENV" python myscript.py
Bowman answered 13/11, 2021 at 11:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.