Anaconda environment bash prefix too long

余生长醉 提交于 2019-12-23 18:06:59

问题


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

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?

My $PS1:

\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$

回答1:


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}) '

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/$

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.

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})) '


来源:https://stackoverflow.com/questions/56619347/anaconda-environment-bash-prefix-too-long

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!