显示虚拟环境

发布于 2024-11-26 15:14:44 字数 280 浏览 0 评论 0原文

当我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

简单 2024-12-03 15:14:44

所以我想出了如何做到这一点。在激活脚本中,$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 the activate script that you run to activate the virtualenv ([yourenv]/bin/activate]). There you change the line that that defines the new $PS1 from PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1" to be whatever you want, here PS1="(F)$PS1".

北方的韩爷 2024-12-03 15:14:44

您需要设置 $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 with F.

薄荷→糖丶微凉 2024-12-03 15:14:44

作为替代方案,请使用 pew (Python 环境包装器)。那么当您使用 virtualenv 时,您的 PS1 就不会被破坏,并且您可以在 .bashrc (等)中将您的 PS1 设置为通常,显示 $VIRTUAL_ENV(如果已设置)。 我的的相关部分如下所示:

# python virtual env, however it comes to be
if [ -z ${VIRTUAL_ENV+x} ]
then
VENV_NOTICE=""
else
VENV_NOTICE=" (py: $(basename "$VIRTUAL_ENV"))"
fi

PS1='whatever $VENV_NOTICE else'

As an alternative, use pew (Python Env Wrapper). Then your PS1 won't get mangled when you use a virtualenv, and you can set your PS1 in your .bashrc (etc.) as usual, displaying the $VIRTUAL_ENV if it's set. The relevant piece of mine looks like this:

# python virtual env, however it comes to be
if [ -z ${VIRTUAL_ENV+x} ]
then
VENV_NOTICE=""
else
VENV_NOTICE=" (py: $(basename "$VIRTUAL_ENV"))"
fi

PS1='whatever $VENV_NOTICE else'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文