【问题标题】:Python Console Name CustomizationPython 控制台名称自定义
【发布时间】:2010-08-17 08:52:55
【问题描述】:

通常 Python 控制台是这样的:

>>> command

有没有办法让它看起来像:

SomeText>>> command

【问题讨论】:

    标签: python console command-line-interface customization


    【解决方案1】:
    sys.ps1 == ">>>"
    sys.ps2 == "..."
    

    您也可以在PYTHONSTARTUP 环境变量中进行更改。

    例如(这不安全),将以下内容放入脚本中的某个位置,并将 PYTHONSTARTUP 设置为指向该脚本。

    import sys
    import getpass
    sys.ps1  = getpass.getuser( ) + ">>> "
    

    当然,您可以将上面的最后一行更改为您想要的任何内容。

    【讨论】:

      【解决方案2】:

      你可以试试

      import sys
      import os
      
      sys.ps1 = os.getenv("USER") + sys.ps1
      

      (或 getpass.getuser() 其他答案的建议)

      在更正的版本中,当然更容易:

      import sys
      sys.ps1 = "SomeText" + sys.ps1
      

      【讨论】:

      • 我更正了这个问题。我不需要显示用户名。只是用于制作一些个性化屏幕截图的自定义文本。
      【解决方案3】:

      IPython 提供了设置复杂提示的便捷方式,如here 所述。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-18
        • 1970-01-01
        • 2017-08-15
        • 2017-04-22
        • 1970-01-01
        • 2021-08-18
        相关资源
        最近更新 更多