在Debian 6中,Python控制台输出被覆盖

时间:2013-09-11 09:47:42

标签: python linux debian

我在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语句在安装过程的输出仍在继续时被执行,因此被覆盖。注意上面两个输出块之间的空白区域 - 这是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%的{​​{1}}部分有关,但我不确定。

我该如何纠正?

1 个答案:

答案 0 :(得分: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=...声明。

希望这有帮助