How do I call a dialog for authentication?

There are two different Q&As at stackoverflow answering your question: here and here.

Both answers use the command gksudo (already mentioned by @khamer). If you run gksudo command (also without python) it will basically do what sudo does, but with a graphical interface - i.e. ask the user for a password and then run the command as root if the user is in the sudoers file.

To implement the suggested solution from the linked answers in python, you can use something as shown by the following example:

Create a file run.py:

#!/usr/bin/python

import subprocess
subprocess.call(['gksudo','python create.py'])

And a file create.py:

#!/usr/bin/python

# Create test file..
f = file("mytestfile", "w")

Then run python run.py and after you enter your password a file owned by root will be created. If you run python create.py it will be owned by you (the file should not exist before running the script).


While I'm sure there's a fancier python solution, the down and dirty version is to use gksudo. gksudo does the same thing sudo does, except it creates that dialog for authentication you mentioned instead of asking on the command line.

See https://help.ubuntu.com/community/RootSudo#Graphical_sudo