Anaconda: Disable Prompt Change

Anaconda: disable prompt change

If you are like me, you like the non-default environment to show up if you have activated it, but don't want to clutter up your prompt in other cases -
(e.g. you happen to use bash for reasons having nothing to do with python)

Place the following excerpt in your ~/.bash_profile right after the section managed by conda:

# ahj - remove the default Python environment artifact "(base)" from prompt
PS1=$(echo "$PS1" | perl -pe 's/^\(base\)\s*//' )

How to remove (base) from terminal prompt after updating conda

Use the base env's activation hook

For each env, any scripts in the etc/conda/activate.d directory will be executed post-activation (likewise etc/conda/deactivate.d scripts for deactivation). If you add a script to remove the (base), similar to @ewindes suggestion, you'll get the behavior you desire.

I had to create this directory for base, which is just the root of your Anaconda/Miniconda folder. E.g.,

mkdir -p miniconda3/etc/conda/activate.d

Then made a simple file in there (e.g., remove_base_ps1.sh) with one line:

PS1="$(echo "$PS1" | sed 's/(base) //') "

If you are using zsh, use this instead.

PROMPT=$(echo $PROMPT | sed 's/(base) //')

Launching a new shell then does not show (base), and deactivating out of nested envs also takes care of the PS1 change.

Note: You must add quotes around $PS1 if you want to preserve ending spaces.

How do I prevent Conda from activating the base environment by default?

I have conda 4.6 with a similar block of code that was added by conda. In my case, there's a conda configuration setting to disable the automatic base activation:

conda config --set auto_activate_base false

The first time you run it, it'll create a .condarc in your home directory with that setting to override the default.

This wouldn't de-clutter your .bash_profile but it's a cleaner solution without manual editing that section that conda manages.

How to modify conda prompt string content

There is a proper and very simple way to do this using conda's native
configuration options.

Edit the .condarc file to contain the line:

env_prompt: \n({default_env})

or run the command:

$ conda config --system --set env_prompt "\n({default_env})"

Either of these will achieve the desired effect for new terminals.
Note that the --system option may not be desirable for many use cases.
See the explanation below for more details.



From the conda documentation

This feature can be elusive if you don't know where to look. The most
natural way to find it is to start with the configuration section
of the conda user guide.

The "using the .condarc conda configuration file" overview tells
us:

The conda configuration file, .condarc, is an optional runtime
configuration file that allows advanced users to configure various
aspects of conda, such as which channels it searches for packages,
proxy settings, and environment directories. For all of the conda
configuration options, see the configuration page.

The configuration page describes the setting we want, along with
its default value:

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


The conda config command

The conda config command is quite useful on its own. Doing

$ conda config --describe

shows the same information as the configuration page.

Since my .condarc file is in a non-default location, I use the --system option
for conda config to prevent conda from creating a new .condarc file in my
home directory. From conda config --help:

Config File Location Selection:
Without one of these flags, the user config file at '$HOME/.condarc' is used.

--system Write to the system .condarc file at
'<my-anaconda-install-path>/.condarc'.
--env Write to the active conda environment .condarc file
(<current-env-path>). If no
environment is active, write to the user config file
($HOME/.condarc).
--file FILE Write to the given file.

switch between anaconda virtual environments and uses of activate/deactivate/remove

when you open the powershell, you will be on the default powershell window: PS C:\Users\some_user>

Then if you type conda activate base you will see the following line: (base) PS C:\Users\some_user

Now you are in the base environment which comes default with conda. If you want to switch to another environment you could simply type: conda activate myenv within the base environment.

(base) PS C:\Users\some_user> conda activate myenv
(myenv) PS C:\Users\some_user>

Now when you deactivate myenv it will go back to base environment.

(myenv) PS C:\Users\some_user> conda deactivate
(base) PS C:\Users\some_user> conda deactivate
PS C:\Users\some_user>

Activating environments is essential to making the software in the
environments work well. Activation entails two primary functions:
adding entries to PATH for the environment and running any activation
scripts that the environment may contain. These activation scripts are
how packages can set arbitrary environment variables that may be
necessary for their operation. You can also use the config API to set
environment variables.
(https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html)

When you are within an environment, the packages within that environment can be accessible. When you switch or deactivate from an environment, those packages specific to that environment will not be accessible from powershell.

When you remove an environment, it means you permanently remove environment and all the packages belong to that environment from the computer.

PS C:\Users\some_user> conda remove --name myenv --all

Remove all packages in environment C:\Users\some_user\.conda\envs\myenv:

No packages found in C:\Users\some_user\.conda\envs\myenv. Continuing environment removal

how to revert back to default behavior of env_prompt parameter in .condarc?

Solution

It turns out that you need to do the following to get back the default behavior.
Here we set the default behavior of env_prompt variable in .condarc file, again before deactivating the environment that was installed at a non-default location.

Assuming you create your python-environment directory (.env) under your project directory as follows:

conda env create --prefix ./.env -f envirnment.yml

Follow these steps for activating and deactivating the environment.

# for activating env
conda config --set env_prompt '({name})'
conda activate ./.env

# for deactivating env
conda config --set env_prompt '({default_env})'
conda deactivate
conda activate base

Description of the env_prompt variable

Source: conda-config: .condarc file

### .condarc file (env_prompt section)

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

Remove anaconda environment prefix from ubuntu terminal command prompt

Well, I found the answer myself. Anaconda has nice feature for this also:

conda config --set changeps1 false

It hides the prefix in your command prompt.



Related Topics



Leave a reply



Submit