0

我在 python 中有一个小脚本,它可以在 Debian 6 (Python 2.6.6) 中使用 apt-get 自动安装一些软件包,如 wget、git。然后脚本安装pip,然后使用pip,安装请求phpserialize。以下是脚本运行时得到的输出:

root@ffVMdeb64:~# python test.py 
Reading package lists... Done
mkdir: cannot create directory `/usr/local/src/forpip': File exists
Building dependency tree       
Reading state information... Done
git is already the newest version.
wget is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Reading package lists... Done
Building dependency tree... 50%


Building dependency tree       
Reading state information... Done
Requirement already satisfied (use --upgrade to upgrade): phpserialize in /usr/local/lib/python2.6/dist-packages
Cleaning up...
Requirement already satisfied (use --upgrade to upgrade): requests in /usr/local/lib/python2.6/dist-packages
Cleaning up...
git is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

最后,脚本会从用户那里获取一些输入。但是,该raw_input语句会在安装过程的输出仍在进行时执行,因此会被覆盖。请注意上面 2 个输出块之间的空格 - 这是raw_input语句被打印然后被覆盖的地方。

脚本的相关部分如下:

subprocess.call("pip install phpserialize &> /dev/null 2>&1", shell=True)
subprocess.call("pip install requests &> /dev/null  2>&1", shell=True)
subprocess.call("apt-get install git -y &> /dev/null 2>&1", shell=True)
import phpserialize
import requests
from phpserialize import serialize
from phpserialize import unserialize

def checktext():
    text = raw_input("\n\n\nEnter your text:")
    return text

itext = checktext()

我在 CentOS 6.3 和 6.4 中测试了完全相同的脚本,它按预期工作。我想这与Building dependency tree... 50%部分有关,apt-get但我不确定。

我该如何纠正?

4

1 回答 1

0

这可能不是确切的解决方案,我很确定会有更好的解决方案,但我认为如果您sleep在最后一个apt-get语句之后尝试,它可能会起作用。

根据您的代码:

import time
subprocess.call("pip install phpserialize &> /dev/null 2>&1", shell=True)
subprocess.call("pip install requests &> /dev/null  2>&1", shell=True)
subprocess.call("apt-get install git -y &> /dev/null 2>&1", shell=True)
time.sleep(5)
import phpserialize
import requests
from phpserialize import serialize
from phpserialize import unserialize

def checktext():
    text = raw_input("\n\n\nEnter your text:")
    return text

itext = checktext()

这会导致整体apt-get首先执行,然后继续执行您的text=...声明。

希望这可以帮助

于 2013-09-11T09:59:16.123 回答