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
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).
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.
I would think it's obvious that it should uninstall all packages when removing an environment, since how would they be accessed otherwise, but I haven't seen documentation saying so, so I'm checking here if all packages need to be removed first.
Let's be more specific and remove the env foo located at anaconda3/envs/foo with
conda env remove -n foo
This usually deletes everything under anaconda3/envs/foo.
PyPI packages may stick around. If you previously used pip install in the environment, it can occasionally leave some residual things behind. If that's the case, you'll need to delete the anaconda3/envs/foo folder manually after conda env remove. Or you could try to pip uninstall any PyPI packages first1, to get a clean conda env remove result.
Conda also caches all packages, independent of whether or not they are currently in use. This would be under anaconda3/pkgs (usually). To additionally delete the packages no longer in use, one can use
conda clean -tp # delete tarballs and unused packages
1: There is a command to programmatically remove all PyPI-installed packages from Conda environments in this answer.
The conda environment will be deleted. Sometimes some packages stay behind, although they are not bound to any environment. You can delete these under
<your anaconda folder> -> envs -> <the env you deleted>.
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 tried to create a new environment for anaconda installation that I want to tweak apart from the original install. I found this not working:
$ conda env create --name pandas018numpy111 pandas018test1
Using Anaconda Cloud api site https://api.anaconda.org
Error: Invalid name, try the format: user/package
pandas018test1 does not exist or can't be accessed
environment.yml file not found
There is no requirements.txt
What is wrong here?
What I wanted to accomplish was to create the copy of the original environment, add some modules for testing around, then toss away the test environment (pandas018numpy111).
Ah, I found the answer.
What I need is the conda create command, actually,
$ conda create -n pandas018numpy111 --clone root
Then I have the throw-away environment to modify, etc and later just toss. To switch then I will just use:
$ source activate pandas018numpy111
and to exit from this environment,
$ source deactivate