Search code examples
pythonlinuxraspberry-piraspbian

Raspbian - Run Python script in venv with sudo rights


I am having trouble writing files to a usb drive in /media/usb via python file.write(). How do I run the python script with sudo ? And, since it should be automatic, how would i avoid to type in the password?


Solution

  • It's nothing about python script, but linux user permissions.

    You can have the following options (choose one),

    • Give your current user the access right to the mounted folder of usb drive
    • Run script with sudo to elevate user privilige

    To avoid password prompt, you can add a sudoers file under /etc/sudoers.d/, e.g.

    echo "$USER ALL=(ALL:ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/$USER