How do I execute a Python program with root access? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How do I execute a Python program with root access?

At last I think I found a code for playing a sound on the raspberry pi, but it stucks in a line where the program needs sudo rights: import os import sys def beep(): audio=file('/dev/audio', 'wb') #in this line it stops cause of no permission count=0 while count<250: beep=chr(63)+chr(63)+chr(63)+chr(63) audio.write(beep) beep=chr(0)+chr(0)+chr(0)+chr(0) audio.write(beep) count=count+1 audio.close() beep()

5th May 2017, 7:26 AM
Johann Leis
Johann Leis - avatar
2 Answers
+ 28
Use: sudo python <filename.py> The sudo command ist for running as admin. After pressing enter you must type in your user and password.
10th Nov 2017, 5:31 AM
Worm
Worm - avatar
+ 8
How do you run your code? Basically, on a linux system, you type "python filename.py" on a terminal ( console ), so to get root access you need to do "sudo python filename.py"... otherwise, you have automated this by just typing the filename and indicate to the console interpreter the location of the python interpreter at first lines of your source code ( commented with the '#' char ), and you need to setup the good rights elsewhere :P ( in theory, you must give the needed rights to the user who run the code... maybe there's a way, through non-cross platform module, to change the user and or the rights at run-time )
5th May 2017, 7:40 AM
visph
visph - avatar