bashanacondapython-venvps1

Anaconda environment bash prefix too long


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\]\$

Solution

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