I have a centos 7 VM and I need sudo permissions to run certain shell commands in a python script. I have a #!/usr/bin/env python3.7 at the top of my script and running it as sudo ./script.py gives me a /usr/bin/env: python3.7: No such file or directory error. How do I run my code as sudo? Just to clarify, it does run if I simply enter ./script.py, but I get a permission denied when the script tried to execute 3 specific commands hence the need for sudo.

echo $PATH result:


sudo echo $PATH gives the same result as above.

type python3.7 gives me: python3.7 is /usr/local/bin/python3.7

  • It appears your PATH includes python3.7 whereas the PATH of root does not. Try echo $PATH and sudo echo $PATH to confirm, they should contain the output of type python3.7
    – Panki
    Jun 23, 2021 at 14:04
  • I understand theoretically what you mean, I just have no idea how to fix it. I added to my post the results from those commands
    – BloodLord
    Jun 23, 2021 at 14:14
  • @Panki isn't $PATH expanded by the shell before sudo is even run? (@BloodLord: #!/bin/env /usr/local/bin/python3.7 should work for the moment, in particular if you're not worried about cross-distro compatibility, even if it's not the nicest solution.) Jun 23, 2021 at 14:19
  • @UlrichSchwarz the script will only be run on centos 7 machines (im not sure if thats what you mean by cross-distro) so I think it should be fine
    – BloodLord
    Jun 23, 2021 at 14:22
  • 1
    What is the output of sudo sh -c 'echo $PATH'? Also, @UlrichSchwarz probably meant #!/usr/local/bin/python3.7 and not #!/bin/env /usr/local/bin/python3.7.
    – terdon
    Jun 23, 2021 at 14:32

1 Answer 1


There are a number of problems with

#!/usr/bin/env python3.7

The first is, that python3.7 must be in the PATH of the user that runs the script. Apparently, for your root-user, this is not the case.

The second is, that it runs the first python3.7 that is in the PATH. This may not be a big issue when you explicitly request version 3.7, but I've encountered a number of #!/usr/bin/env python-s that ran 3 instead of the required 2 etcetera.

That is why I do not recommend using #!/usr/bin/env.

There are 3 solutions to your problem:

  • put /usr/local/bin/ in the PATH of root (not recommended because of the local; that is the reason why it is not as default in the PATH)
  • use #!/usr/local/bin/python3.7 in your script. This will probably break if you use it on other systems where python is under /usr/bin
  • use a default installation of python, where python is under /usr/bin; I would recomend to use only #!/usr/bin/python3 and not the sub-version.
  • So my script requires a version of python3.7 of earlier. Will the 3rd option still work on other machines? Like will it make sure that python3.7 specifically is installed, or juste use whatever version of python3 the host has?
    – BloodLord
    Jun 23, 2021 at 14:48
  • That means that on my installations (Mint Linux), your script breaks (Mint is 3.8). Unless you have very specific reasons why your script must not run on the latest Python, I would suggest making it less version dependent. Otherwise, use the second option. Jun 23, 2021 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.