Skip to main content

A Python class to handle executing system commands using sudo

Project description



A Python module to handle executing system commands using sudo.


Extend GenericConfigure, setting at a minimum an appropriate argv command & argument list.


The following class will check if pip is installed and, if not, install it system-wide using sudo easy_install pip:

from configure_with_sudo import GenericConfigure

class InstallPip(GenericConfigure):

    def __init__(self, kill_sudo_cred=True):
        # Initial argv is simply to check if 'pip' is installed
        argv = ["/usr/local/bin/pip", "-V"]
        super(InstallPip, self).__init__(
            argv, use_sudo=True, kill_sudo_cred=kill_sudo_cred)

            # Execute to check if pip is already installed
            # if it is, self.configured will be set to True
            # and subsequent calls to execute() will do nothing

        except Exception:
            # If pip -V failed, assume pip is not installed.
            # Replace self.argv with the installation command.
            self.argv = ["/usr/bin/easy_install", "pip"]

pip_installer = InstallPip()

The following example will capture the output of sudo ls -l /private/var/root, and print it line-by-line:

class DirectoryLister(GenericConfigure):

    def __init__(self, path):
        argv = ["ls", "-l", path]
        super().__init__(argv, use_sudo=True)

path = "/private/var/root"
lister = DirectoryLister(path)
output = lister.execute(return_output=True)

for line in output:

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for configure-with-sudo, version 0.1.0.dev4
Filename, size File type Python version Upload date Hashes
Filename, size configure_with_sudo-0.1.0.dev4-py3-none-any.whl (5.8 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size configure_with_sudo-0.1.0.dev4.tar.gz (3.7 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page