I would like to delete an Anaconda environment. From this reference, it looks like I could use
conda remove --name myenv --all
or
conda env remove --name myenv
The documentation mentions both, but does not explain the difference.
How might I determine what the --all flag does?
There is no difference in effect.
Conda has two remove commands:
conda remove - for removing packages
conda env remove - for removing environments
Both have a --name,-n argument that specifies the environment on which to operate. Only the former also has an --all flag, which effectively does the same thing as the latter.1
Obsolete (From Original Answer)
The original first example in the question had a typo and was invalid because it indicated to remove package(s) from an environment, but did not specify any packages. Running it would have yielded an error message:
$ conda remove -n myenv
CondaValueError: no package names supplied,
try "conda remove -h" for more details
[1] This is a slightly inconsistent API design, in my opinion. Since one can create an empty environment, I believe a more symmetric result of conda remove --all would be that it remove all the packages but still retain the empty environment. Users that want to operate on a whole environment level should being using conda env commands. Unfortunately, this overlap of functionality is an artifact of ontogeny, namely, conda-env was originally a separate package that came after conda, and so conda remove -n envname --all was the original idiom for environment removal.
Related
I'm trying to save my conda environment so I can send it to others to reproduce my work. Within my activated environment:
(env_name) c:\eric\ conda env export > environment.yml
This environment has dozens of packages installed (including numpy, matplotlib). When I open the resulting environment.yml file, I get only:
name: env_name
channels:
- conda-forge
- defaults
prefix: C:\Users\eric\Miniconda3\envs\env_name\envs\env_name
That is the entire file: there isn't even a line for dependencies: that shows up, which usually does show up for the packages in the env.
I am using miniconda, version 4.12.0, and have run conda update conda.
Conda issues
I found two issues at conda that have come up with this:
https://github.com/conda/conda/issues/8839
https://github.com/conda/conda/issues/10997
One solution at the first issue is to use the following command:
conda env export -p path-to-folder > environment.yml
Unfortunately this did not work for me, but it seems to have worked for many.
Comparison to related question
Note indeed the prefix value is pretty strange it typically is something like:
C:\Users\eric\.conda\envs\env_name
Frankly I am not concerned about that, unlike the related question focused on removing that prefix and the accepted answer does that bash shell commands: Anaconda export Environment file.
My question is more first-order: why aren't dependencies showing up in my yaml file?
e.g., something like the following should be appearing:
dependencies:
- pandas=1.0.3=py37h47e9c7a_0
- qt=5.9.7=vc14h73c81de_0
- pip:
- imageio==2.9.0
- scikit-image==0.17.2
But literally there are zero dependencies, and not even a dependencies argument.
It's possible the environment isn't properly activated and therefore it's not actually exporting what is wanted. Fortunately, most Conda commands provide arguments to specify an environment explicitly. Specifically, try using the --name,-n argument, like
conda env export -n env_name > env_name.yaml
Personally, I try to always use an --name,-n or --prefix,-p flag, because I find context-sensitive commands are more prone to errors (e.g., installing in incorrect environments).
It often happens that when I create a conda environment, I forgot about activating it, despite the clear conda message. And I end up installing my packages in the conda base environment. (yeah... I'm a bit of a dreamer)
My questions are:
Are there use cases where you create a conda environment but don't activate it right after?
How to create a conda environment and activate it in a single line? (in a Linux prompt shell, and non-interactive)
As a concrete example in answer to your first question, I have used conda as a way to wrap a disposable build environment in some Makefile targets, i.e., I create the environment, and then subsequent commands or targets may make use of the environment via conda run.
Adapting a snippet from one Makefile, you could create a function in a bash startup file:
conda_create_and_run() {
ENV_NAME=$1
CONDA_PY_VER=$2
. ${CONDA_ENV_FILE}
conda config --append envs_dirs ${CONDA_DIR}
conda create -p ${CONDA_DIR}/${ENV_NAME} python=${CONDA_PY_VER} -y
conda activate ${ENV_NAME}
}
Here CONDA_PY_VER is the non-default python version you want the environment to possibly be created with, and CONDA_DIR and CONDA_ENV_FILE are, respectively, the location where conda keeps its environments and the conda environment file you need to source (or have part of your shell init) in order to have the conda commands available.
You would then use it as:
conda_create_and_run myenv 3.8
to create an environment for python3.8 named myenv.
I have a conda environment setup already.
I want to delete a few packages out of it, update few and add a few new packages.
currently, I am doing it manually one after another after source activate <my_exiting_conda>.
conda update x
conda update y
conda remove z --force
conda remove w --force
conda install -c <channel> <package name>
Is there any way using which I can put all these commands in a file and just use it to do all work at once.
Also when I run the above command manually, it asks to hit Y/n for each command? how can I avoid that?
Consider switching to using YAMLs to manage your envs and refrain from using conda update/install/remove commands. When you want to make multiple changes to an env, change them in the YAML, then use:
conda env update -f environment.yaml
This command also has the optional argument --prune which will remove any packages that are not required, i.e., provides the package deletion mechanism you seek.
Note that conda env commands do not provide a transaction review step. For that reason, I would not recommend using it to manage the base env.
Starting from Existing Env
To get a working YAML from an existing env (say foo), try running something like
conda env export -n foo --from-history > foo.yaml
The --from-history argument will only include the explicit specs that you've provided to the env, so the YAML will look closer to what one might make and maintain from scratch.
Note that if there were packages installed through pip they won't appear in the --from-history version. In that case, I would still begin from this version, then export a full YAML to capture any pip specs, and add them to the simpler version.
Yes, you can pass packages in one go
conda update x y
conda remove z w --force
I am trying to remove the Py2 environment from my Ananconda 3 installation but in vain.
I follow advice found in the official documentation
I am asked to deactivate the environment but even after I deactivate it I get again the same message. See below:
Try this, maybe it will help:
conda remove -n ENVIRONMENT_NAME --all
Here is a link to the docs.
The problem is that you misunderstood the documentation. It says conda remove --name myenv --all. You interpreted this as meaning you should replace name with the name of your environment. But name is the name of the command option and can't be changed; instead you need to replace myenv with the name of your environment, so that the command becomes
conda remove --name py27 --all
I have a python 2.7 conda environment and would like to create an equivalent environment with python 3.4. I am aware of the --clone option when creating environments, but it won't accept additional arguments, like python=3.4. Is there a way to do this automatically? I thought about trying to use the output from conda list --export, but that also encodes the python release.
One way would be to
conda list --export > exported-packages.txt
And then edit that file to remove the last part of each package with the py27_0 parts (you might also want to remove the versions, in case some version of a package doesn't have a Python 3 version). Then
conda create -n py3clone --file exported-packages.txt
Another idea would be to clone the environment:
conda create -n clonedenv --clone oldenv
conda install -n clonedenv python=3.4
conda update -n clonedenv --all
Note that obviously both of these will fail if you have some package that doesn't have a Python 3 version.
I tried this and had a lot of issues with plot.ly being updated to v. 4. My code was written on 3.10 and didn't feel like changing it. I had to copy the whole environment (every single file from \envs) from one machine to next, it works.