如何自定义virtualenv shell提示 [英] How to customize virtualenv shell prompt

查看:60
本文介绍了如何自定义virtualenv shell提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何定义激活 Python虚拟环境时要使用的自定义提示?

我有一个bash脚本,用于激活在调用特定 Fabric 命令时使用的virtualenv.我希望外壳程序提示符说类似(fab)"的内容,这样我就可以轻松地将其与我打开的其他外壳程序区分开.在示例中,我尝试了:

 #!/bin/bashscript_dir =`目录名$ 0`cd $ script_dir/bin/bash -c".env/bin/activate; PS1 ='(fab)'; exec/bin/bash -i" 

,但提示没有任何变化.我在做什么错了?


解决方案

在virtualenv的激活脚本(位于virtualenv下的bin文件夹中)中设置了提示.如果只想更改提示几次,则可以在调用Activate之前设置一个环境变量(确保在相应的停用文件中将其清除).如果您只是想让提示始终与众不同,则可以在看起来像这样的行中激活

 设置"PROMPT =(virtualenvname)%PROMPT%" 

如果您使用virtualenvwrapper,则也可以在postactivate和postdeactivate脚本中完成所有这些操作.

How do you define a custom prompt to use when activating a Python virtual environment?

I have a bash script for activating a virtualenv I use when calling specific Fabric commands. I want the shell prompt to say something like "(fab)" so I can easily distinguish it from other shells I have open. Following this example, I've tried:

#!/bin/bash
script_dir=`dirname $0`
cd $script_dir
/bin/bash -c ". .env/bin/activate; PS1='(fab) '; exec /bin/bash -i"

but there's no change to the prompt. What am I doing wrong?


解决方案

The prompt is set in the virtualenv's activate script (located in the bin folder under the virtualenv). If you only want to change the prompt some times, you could set an environment variable before calling activate (make sure to clear it in the corresponding deactivate file). If you simply want the prompt to be different all the time, you can do that right in activate at the line that looks like

set "PROMPT=(virtualenvname) %PROMPT%"

If you're using virtualenvwrapper, you could do all of this in the postactivate and postdeactivate scripts as well.

这篇关于如何自定义virtualenv shell提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆