Request Help With pkexec

Wildman best_lay at yahoo.com
Wed Apr 5 17:10:37 EDT 2017


On Mon, 03 Apr 2017 14:29:56 -0500, Wildman wrote:

> Python 3.4.2
> Tkinter 8.6
> GCC 4.9.1 on Linux
> 
> I am working on a gui program using Tkinter. The program will
> have a feature to restart as root.  I am testing different gui
> front-ends from a terminal to raise privileges and I want to
> be able to use as many as possible for obvious reasons.  Gksu,
> kdesudo and su-to-root all work perfectly.  However, I have a
> problem with pkexec.  Here is the command I am using from a
> terminal:
> 
> $ pkexec python3 /home/user/Python/linfo-tk/linfo-tk.py
> 
> I get this error:
> 
> Traceback (most recent call last):
>   File "/home/user/Python/linfo-tk/linfo-tk.py", line 455, in <module>
>     root = tk.Tk()
>   File "/usr/lib/python3.4/tkinter/__init__.py", line 1854, in __init__
>     self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
> _tkinter.TclError: no display name and no $DISPLAY environment variable

I am posting this follow-up for any Linux developer that might
be working on a gui program that requires root permissions.
My research revealed that programs like gksu are not going to
be supported long term.  Some distros have already dropped
gksu in favor of pkexec.

Pkexec is part of the PolicyKit package, policykit-1.  By
default it will not work with gui (X11) programs but that
behavior can be changed by using a .policy file placed in
/usr/share/polkit-1/actions/.  Once I created such a file,
pkexec worked perfectly with my program.  So my problem
was not Python related.

Below is the actual .policy file I am using.  It could be
edited to work with any program.  Lots of info on the
web about pkexec and the HTML .policy file.  Just search
on something like "run program with pkexec".  The name
of the file is com.ubuntu.pkexec.linfo-tk.policy.  Here
is the contents:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC
 "-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/PolicyKit/1/policyconfig.dtd">

<policyconfig>

  <vendor>Wildman Productions</vendor>
  <action id="org.freedesktop.policykit.pkexec.linfo-tk.py">
    <message>Authentication is required to run Linfo-tk as root</message>
    <defaults>
      <allow_any>auth_admin</allow_any>
      <allow_inactive>auth_admin</allow_inactive>
      <allow_active>auth_admin</allow_active>
    </defaults>
    <annotate key="org.freedesktop.policykit.exec.path">/opt/linfo-tk/linfo-tk.py</annotate>
    <annotate key="org.freedesktop.policykit.exec.allow_gui">TRUE</annotate>
  </action>

</policyconfig>


-- 
<Wildman> GNU/Linux user #557453
The cow died so I don't need your bull!


More information about the Python-list mailing list