Anaconda环境bash前缀太长 [英] Anaconda environment bash prefix too long
问题描述
我在项目文件夹中创建了anaconda环境,该文件夹使用-p选项指定了路径,即不在默认的anaconda3/envs文件夹中:
I created an anaconda environment in a project folder specifying the path with -p option, i.e. not in the default anaconda3/envs folder:
conda create -p venv
问题是,当我激活该环境时,终端中的bash前缀太长,即它将环境的整个路径添加到提示中:
The problem is that when I activate that environment, the bash prefix in the terminal is too long, i.e. it prepends the entire path of the environment to the prompt:
(/path/to/the/environment/venv) user@machine: ~/path/to/environment/$
有没有办法解决这个问题,这意味着将其缩短或从提示中删除前缀?
Is there a way to fix this, meaning make it shorter or delete the prefix from prompt?
我的$ PS1:
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$
推荐答案
Conda提示自定义
自 Conda v4.6.0 提供了env_prompt
配置选项,可以自定义PS1更改.这是说明:
Conda Prompt Customization
Since Conda v4.6.0 there has been the env_prompt
configuration option to provide for customization of the PS1 change. Here is the description:
$ conda config --describe env_prompt
# # env_prompt (str)
# # Template for prompt modification based on the active environment.
# # Currently supported template variables are '{prefix}', '{name}', and
# # '{default_env}'. '{prefix}' is the absolute path to the active
# # environment. '{name}' is the basename of the active environment
# # prefix. '{default_env}' holds the value of '{name}' if the active
# # environment is a conda named environment ('-n' flag), or otherwise
# # holds the value of '{prefix}'. Templating uses python's str.format()
# # method.
# #
# env_prompt: '({default_env}) '
一个可以帮助您解决问题的方法是仅使用{name}
变量
One option that would help with your case would be to just use the {name}
variable
conda config --set env_prompt '({name}) '
将仅显示环境的文件夹名称.例如,您的示例为
which will show only the env's folder name. E.g., your example would be
(venv) user@machine: ~/path/to/environment/$
注意,这样做会使得当 base env处于活动状态时,提示将显示(anaconda3)
而不是(base)
;否则,其他命名的envs应该照常出现.
Note, this will make it so that when the base env is active the prompt will show (anaconda3)
instead of (base)
; otherwise, the other named envs should appear as usual.
如果您真的不能忍受,可以在未命名的env上运行basename {default_env}
以获得与{name}
相同的结果,并且仍然保留base
.也就是说,
If you really can't stand that, you could run basename {default_env}
to get the same result as {name}
on unnamed envs and still retain base
. That is,
conda config --set env_prompt '(\$(basename {default_env})) '
这篇关于Anaconda环境bash前缀太长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!