2

I am attempting to call python within a bash script, but the script cannot find it. I have not done bash script programming for over 30 years, so I am not currently adept at bash script debugging. I am using pyenv to select the version of python to use within a particular directory. Here is the setup I am using:

dnessett@homelserv:~/SaltSource/scripts$ ls python_on_root.sh dnessett@homelserv:~/SaltSource/scripts$ cat python_on_root.sh #!/bin/bash sudo python "$@" dnessett@homelserv:~/SaltSource/scripts$ pyenv local 3.10.15 dnessett@homelserv:~/SaltSource/scripts$ python --version Python 3.10.15 dnessett@homelserv:~/SaltSource/scripts$ ./python_on_root.sh sudo: python: command not found 

Since I am using sudo within the script to call python, here is the entry in /etc/sudoers that I had hoped would allow sudo to execute the local python interpreter:

#add python to sudo list dnessett homelserv=(root)NOPASSWD:/home/dnessett/.pyenv/versions/3.10.15/bin/python 

dnessett is the username and homelserv is the machine name. The pathname following NOPASSWD is where the python version 3.10.15 is located.

The whole point of this is to use the python_on_root.sh script as the interpreter for a Eclipse/PyDev project (see: Stack Overflow thread).

As is apparent from the output given above, sudo cannot find python, even though I have set it as the local python interpreter within ~/SaltSource/scripts. I imagine there is some interaction between the script and sudo that is preventing the latter from finding the intended python interpreter.

Can someone see the problem and advise me how to correct it?

2
  • 1
    What are you trying to achieve with that sudoers line? If your user has sudo access, you don't need anything more unless you want this command to run with sudo, as the root user and without prompting for a password.
    – terdon
    CommentedOct 8, 2024 at 21:43
  • Why don’t you activate the environment in the script that uses Python, and then call the script with sudo?
    – Kusalananda
    CommentedOct 9, 2024 at 6:45

2 Answers 2

5

The PATH sudo inherits will be different from your own and likely doesn't include wherever python is installed. You could call it by the full path in your script, ie:

#!/bin/bash sudo /home/dnessett/.pyenv/versions/3.10.15/bin/python "$@" 

However there may be some other unwanted issues with running python as root such as your HOME and PYTHONPATH env variables being different so you may want to run sudo -E instead, however you should still point to the full path of python either way.

Finally you probably shouldn't run python as root at all. Chances are whatever you're trying to do can be solved in a different, more correct way such as changing file/directory permissions to allow your user to accomplish the tasks required.

7
  • 3
    And if this really does need root, it's probably better to call the bash script with sudo instead of using sudo in the script to call python.
    – terdon
    CommentedOct 8, 2024 at 21:41
  • @terdon: I can't call the bash script with sudo, since the bash script is an input to the Eclipse/PyDev setup. See: stackoverflow.com/questions/79044714/….
    – dnessett
    CommentedOct 8, 2024 at 23:42
  • @dnessett theoretically either would work since python should be in the PATH with your user's environment but it never hurts to explicitly specify the full path of a program in your scripts, especially programs like python where you might have multiple versions installed and want to be deliberate about the version used.
    – jesse_b
    CommentedOct 8, 2024 at 23:47
  • @dnessett just delete the erroneous comment and write another comment
    – jsotola
    CommentedOct 8, 2024 at 23:57
  • @jesse_b: (reposting comment to which jesse_b replies above) When you say use sudo -E instead do you mean: sudo -E /home/dnessett/.pyenv/versions/3.10.15/bin/python "$@" or sudo -E python "$@". Added later: Rereading your comment, it must be the first. However, I need to run python as root, since the application being debugged with Eclipse/PyDev needs to run as root.
    – dnessett
    CommentedOct 8, 2024 at 23:59
1

Following on from the previous answer, the changes I needed to make were:

  • add python to sudo list

    dnessett homelserv=(root)NOPASSWD:SETENV:/home/dnessett/.pyenv/versions/3.10.15/bin/python 

    The extra SETENV: after NOPASWD: overrides the prohibition of preserving the environment. I found this here: Stack Exchange Superuser post

  • Then, as specified by jesse_b in the previous answer, the script is modified to be:

    sudo -E /home/dnessett/.pyenv/versions/3.10.15/bin/python "$@" 

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.