显示虚拟环境
当我在 virtualenv 中并且它处于活动状态时,环境名称将显示在正常命令行提示符之前的括号中。它看起来像: (foo-env)User:~/Development/foo-env/foo$
其中 foo-env
是环境的名称。我想知道是否有一种方法可以使命令行提示符显示类似于 (F)User:~/Development/foo-env/foo$
的内容,而不是当前显示的 >(foo-env)
。如果这是可能的,我将如何去做呢?
When I am in my virtualenv and it's active, the name of the environment appears in parentheses before the normal command line prompt. It looks like: (foo-env)User:~/Development/foo-env/foo$
where foo-env
is the name of the environment. I was wondering if there was a way to make it that the command line prompt displayed something like (F)User:~/Development/foo-env/foo$
as opposed to the current display with (foo-env)
. If this is possible how would I go about doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所以我想出了如何做到这一点。在激活脚本中,
$PS1
被重新定义以添加环境名称,在本例中是(foo-env)
。为了在它前面添加您想要的任何内容,您必须进入您运行的activate
脚本来激活virtualenv
([yourenv]/bin/activate] )。在那里,您将定义新
$PS1
的行从PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1"
更改为您想要的任何内容,这里PS1="(F)$PS1"
。So I figured out how to do this. In the activate script the
$PS1
is redefined to prepend the name of the env, in this case(foo-env)
. In order to prepend it with whatever you want you have to go into theactivate
script that you run to activate thevirtualenv
([yourenv]/bin/activate]
). There you change the line that that defines the new$PS1
fromPS1="(`basename \"$VIRTUAL_ENV\"`)$PS1"
to be whatever you want, herePS1="(F)$PS1"
.您需要设置 $PS1 环境变量来更改提示符。
查看 virtualenv 配置文件中的设置。
请参阅 http://www.cyberciti.biz/ Tips/howto-linux-unix-bash-shell-setup-prompt.html 了解如何根据需要进行设置。听起来您只需要将字符串
foo-env
替换为F
即可。You need to set the $PS1 environmental variable to change your prompt.
Take a look in the virtualenv config files for the setting.
See http://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html for how to set it as you'd like. It sounds like you just need to replace the string
foo-env
withF
.作为替代方案,请使用 pew (Python 环境包装器)。那么当您使用 virtualenv 时,您的
PS1
就不会被破坏,并且您可以在.bashrc
(等)中将您的PS1
设置为通常,显示$VIRTUAL_ENV
(如果已设置)。 我的的相关部分如下所示:As an alternative, use pew (Python Env Wrapper). Then your
PS1
won't get mangled when you use a virtualenv, and you can set yourPS1
in your.bashrc
(etc.) as usual, displaying the$VIRTUAL_ENV
if it's set. The relevant piece of mine looks like this: