在 Python 脚本中安装 ubuntu 软件包

在 Python 脚本中安装 ubuntu 软件包

我想在我的python脚本中安装以下包:

python-pip python-sqlalchemy mongodb python-bson python-dpkt python-jinja2 
python-magic python-gridfs python-libvirt python-bottle python-pefile
python-chardet git build-essential autoconf automake libtool dh-autoreconf 
libcurl4-gnutls-dev libmagic-dev python-dev tcpdump libcap2-bin virtualbox 
dkms python-pyrex

我编写了以下代码,但它不起作用。我该如何解决这个问题?

    self.command = "apt install"
    self.packages = "python-pip python-sqlalchemy mongodb python-bson python-dpkt python-jinja2 python-magic python-gridfs python-libvirt python-bottle python-pefile python-chardet git build-essential autoconf automake libtool dh-autoreconf libcurl4-gnutls-dev libmagic-dev python-dev tcpdump libcap2-bin virtualbox dkms python-pyrex"

    print("[+] Installation of the ubuntu packages is starting:")
    for items in packages:
        subprocess.run(str(command.split()) + str(items), stdout=DEVNULL, stderr=DEVNULL)
        print("\[+] Package {} Installed".format(str(self.items)))

答案1

您那里有几个问题:

  • self缺少for items in packages:
  • self.packages是一个字符串,调用时for item self.packages会遍历该字符串的每个字符。您应该从开头将包声明为列表,或者.split()在结尾添加。
  • 你的self.packages代码太长了,不符合 PEP8 每行 79 个字符的标准。

答案2

固定的 :

def package_installation(self):
    self.apt = "apt "
    self.ins = "install "
    self.packages = "python-pip python-sqlalchemy mongodb python-bson python-dpkt python-jinja2 python-magic python-gridfs python-libvirt python-bottle python-pefile python-chardet git build-essential autoconf automake libtool dh-autoreconf libcurl4-gnutls-dev libmagic-dev python-dev tcpdump libcap2-bin virtualbox dkms python-pyrex"

    self.color.print_green("[+] Installation of the ubuntu packages is starting:")

    for self.items in self.packages.split():
        self.command = str(self.apt) + str(self.ins) + str(self.items)

        subprocess.run(self.command.split())
        self.color.print_blue("\t[+] Package [{}] Installed".format(str(self.items)))

相关内容