使用tqdm的python进度栏不能停留在一行上 - python

我正在尝试运行一个脚本,该脚本试图通过p管理在centos7系统上安装模块。
我想为运行脚本时发生的安装实现进度条。
我正在使用tqdm模块来执行此操作。
这是我如何实现模块的一个小贴士:

from tqdm import tqdm
for i in tqdm(commands):
    res = run_apply(i)

在这里,run_apply()是实际处理运行和应用人偶配置的函数。

到目前为止一切顺利,我得到了一个进度条,但是当执行消息被写入控制台时,它仍在控制台中向下移动。
但是,我需要进度条在控制台底部保持恒定,并动态更新,而运行消息不会干扰进度条。
我希望控制台上与执行相关的消息能够按需继续,但是进度条应该从执行开始到结束始终停留在底部。

以下是我所看到的:

        File line: 0.00
          Package: 0.05
          Service: 0.19
             File: 0.23
             Exec: 0.23
         Last run: 1470308227
   Config retrieval: 3.90
            Total: 4.60
Version:
           Config: 1470308220
           Puppet: 3.7.3
now here x
result:  2
 38%|█████████████████████████████████████▋                                                            | 5/13 [00:29<00:51,  6.44s/it]about to:  profiles::install::download_packages
about to run puppet apply --summarize  --detailed-exitcodes --certname puppet -e "include profiles::install::download_packages"
Error: Could not find class profiles::install::download_packages for puppet on node puppet
Error: Could not find class profiles::install::download_packages for puppet on node puppet
now here x
result:  1
 46%|█████████████████████████████████████████████▏                                                    | 6/13 [00:32<00:36,  5.27s/it]about to:  profiles::install::install
about to run puppet apply --summarize  --detailed-exitcodes --certname puppet -e "include profiles::install::install"
Error: Could not find class profiles::install::install for puppet on node puppet
Error: Could not find class profiles::install::install for puppet on node puppet
now here x
result:  1
 54%|████████████████████████████████████████████████████▊                                             | 7/13 [00:34<00:26,  4.45s/it]about to:  stx_network
about to run puppet apply --summarize  --detailed-exitcodes --certname puppet -e "include stx_network"
Notice: Compiled catalog for puppet in environment production in 0.84 seconds
Notice: /Stage[main]/Stx_network/Tidy[purge unused nics]: Tidying File[/etc/sysconfig/network-scripts/ifcfg-lo]
...  

请让我知道我如何实现我想要的。

参考方案

对于要在进度条上方打印的消息,您需要向tqdm发出正在打印消息的信号(否则tqdm或任何其他进度条,都不知道您正在进度条旁边输出消息)。

为此,您可以使用tqdm.write(msg)而不是print(msg)打印消息。如果不想修改run_apply()以使用tqdm.write(msg)而不是print(msg),则可以redirect all standard output through tqdm from the toplevel script as described here。

Python:无法识别Pip命令 - python

这是我拍摄的屏幕截图。当我尝试在命令提示符下使用pip时,出现以下错误消息:pip无法识别为内部或外部命令,可操作程序或批处理文件。我已经检查了这个线程:How do I install pip on Windows?我所能找到的就是我必须将"C:\PythonX\Scripts"添加到我的类路径中,其中X代表python版本。如您在我的…

Python GPU资源利用 - python

我有一个Python脚本在某些深度学习模型上运行推理。有什么办法可以找出GPU资源的利用率水平?例如,使用着色器,float16乘法器等。我似乎在网上找不到太多有关这些GPU资源的文档。谢谢! 参考方案 您可以尝试在像Renderdoc这样的GPU分析器中运行pyxthon应用程序。它将分析您的跑步情况。您将能够获得有关已使用资源,已用缓冲区,不同渲染状态上…

Python:图像处理可产生皱纹纸效果 - python

也许很难描述我的问题。我正在寻找Python中的算法,以在带有某些文本的白色图像上创建皱纹纸效果。我的第一个尝试是在带有文字的图像上添加一些真实的皱纹纸图像(具有透明度)。看起来不错,但副作用是文本没有真正起皱。所以我正在寻找更好的解决方案,有什么想法吗?谢谢 参考方案 除了使用透明性之外,假设您有两张相同尺寸的图像,一张在皱纹纸上明亮,一张在白色背景上有深…

Python uuid4,如何限制唯一字符的长度 - python

在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…