1

Is it possible to run some of the calls of the python-apt module as root rather than having to run the whole script as root?

for example

import apt
cache=apt.Cache()
pkg = cache["p7zip-full"]
print "about to install:"+pkg_name
#switch to root   
if pkg.is_installed:
  print "{pkg_name} already installed".format(pkg_name)
else:
  pkg.mark_install()
#back to user
print "install complete"

instead of having to:

sudo .install.py

thanks alexis

For the duplicate question point: I could be wrong but the answers given in that question use the subprocess module which starts a new process to run sudo commands on the OS. I am trying to make calls to the python-apt module/library as sudo or at least give python-apt the rights to be sudo.

alexis
  • 1,022
  • 3
  • 16
  • 44
  • @ShraddheyaShendre the post you link to is using a subprocess to run cmd as sudo's. I am asking if i can do this within a module – alexis Jun 29 '17 at 09:17
  • @ShraddheyaShendre how could i use `subprocess.call()` to call a module method like `pkg.mark_install()` which is imported from the `apt` module ? – alexis Jun 29 '17 at 09:47
  • @ShraddheyaShendre this give me the error `sudo: pkg.mark_install(): command not found` which is what i would expect since `pkg.mark_install()` is not a cmd you can run from the terminal. As far as I am aware [subprocess](https://docs.python.org/2/library/subprocess.html) is used to spawn a new process you can use to run OS cmd's – alexis Jun 29 '17 at 10:14
  • I don't really know what the `pkg` is. Is it some module, some variable? Your code in the question itself isn't working. Can you post the whole working (and minimum) code, which would work after `sudo .install.py`? – Shraddheya Shendre Jun 29 '17 at 10:23
  • @ShraddheyaShendre OK i have added more code. Hopefully that's clearer. The [apt](https://apt.alioth.debian.org/python-apt-doc/library/) doc i am using – alexis Jun 29 '17 at 10:39
  • why dont you run this snippet as sudo? that will be all you need – Netwave Jun 29 '17 at 11:06
  • @DanielSanchez snippet above is just an example. The actual code is much more complex and the commands needing sudo are spread out the code. – alexis Jun 29 '17 at 11:14

0 Answers0