1

I want to MOVE a conda environment, but there is no conda command to do that.

I GUESS this because conda "hard-links" packages to the environment and runtime config variables, and conda index or Anaconda index and hard-links are not updated correctly. I make this assumption based on conda create --clone v.s. copying the environment directly (Someone please clarify if this assumption is wrong.)

So a workaround for this current conda limitation (no "move" command) is to "build an identical environment" from the parent environment. I followed these instructions Building Identical conda environments:

conda create --name myenv --file spec-file.txt

but they don't work because I get a conda error:

conda-script.py create: error: argument -n/--name: not allowed with argument -p/--prefix:

This is a two-step process:

  1. create a specification file that lists all packages and versions in the parent environment:

    conda list --explicit > spec-file.txt

  2. use this package specification list (the file 'spec-file.txt') to recreate the parent environment, but use a NEW name for the child environment, since the conda index needs a unique name. (Again, someone please clarify if this is a wrong assumption.)

But the following command with location and environment name gives an error (target location "C:\ProgramData\Anaconda3\envs" ; new environment name is "newenv2").

conda create --prefix "C:\\ProgramData\\Anaconda3\\envs"  --name topss2 --file spec-file.txt

ERROR:

conda-script.py create: error: argument -n/--name: not allowed with argument -p/--prefix

I want to be able to do this in one command and specify the new (named) environment packages from spec-file.txt, and specify the location myself (not taking the Anaconda default, which in my case is C:\\Users\\richadmin\\.conda\\envs\\topss).

IT WORKS, BUT NOT WITHOUT MANUAL STEPS

I finally got it to work by using conda create --name myclone --clone myenv. However, I had to manually change to the target directory and then execute the clone command without the spec-file or target directory name. I want to do it in one step to use it on a command line, script or docker build.

QUESTION: Does anyone know if options exist for this:

conda create --name myclone --clone clone_env_name --prefix target_directory

Where is conda --move in the conda command set?

We need a conda "move environment" command in the core conda package. It is easy to accidentally create new environments in the wrong locations. A conda "move" command should just update config variables and hard-links. This would stop wasting file space and time cloning and deleting incorrectly-located environments. (Yeah, I know, "use the command line correctly in the first place", or understand Anaconda's intricate and hidden configuration interface.) This is a common newbie and intermediate-level Python environment usage error that will benefit from a more complete conda command set.

Anaconda uses its own default directories and this causes problems for Python users in corporate IT environments, because corporate IT wants to install files in places like: C:\Users\richadmin\AppData\Roaming or c:\users\username\.conda\envs, or c:\program files\.

I typically use c:\programdata\anaconda3\envs because then this doesn't bump heads with file permission and execution issues in corporate environments.

Rich Lysakowski PhD
  • 2,702
  • 31
  • 44

1 Answers1

1

No Such Command

There is no explicit way to use both the --name and --prefix arguments, the reason being that using the --name argument implicitly defines that the prefix will be to create a folder with the name inside of the default envs_dirs directory.

Workaround

However, a little secret is that Conda will automatically consider any folders within an envs_dirs directory to have the name of the folder. Hence, one workaround to moving an env to another directory, but still have it be referenceable by name, is to include the other directory in the envs_dirs. One thing to keep in mind though, is to make sure the list of envs_dirs maintains the priority you want for where to place a named-env by default.

Example

First, let's check my current configuration

conda config --show envs_dirs
envs_dirs:
  - /Users/user/miniconda3/envs
  - /Users/user/.conda/envs

So, by default all named envs get created in the first directory. Let's say we want to add another directory for envs, but not make it the default. We'll put it in /Users/user/test_envs, and can do it with:

mkdir -p /Users/user/test_envs
conda config --append envs_dirs /Users/user/test_envs

Checking the new configuration shows

conda config --show envs_dirs
envs_dirs:
  - /Users/user/miniconda3/envs
  - /Users/user/test_envs
  - /Users/user/.conda/envs

showing it is now included, but should not override the default location.

Now we can clone an existing env (say, foo) into this location, but we still need to use the --prefix flag:

conda create --clone foo --prefix /Users/user/test_envs/bar

and this enables us to activate this env by name, e.g.,

conda activate bar

This still doesn't technically move the original env that was cloned, but you are free to remove it.

Additional Notes

Following the above procedure, we should preserve an ability to create environments in the default location by name, e.g.,

conda create -n baz

which would create an environment at /Users/user/miniconda3/envs/baz. If one wanted to make the new location the default for named envs, the --append command above should be replaced by the --prepend flag.

None of this will make any difference in saving space. However, be aware that if moving to a different filesystem (say /c drive to /d drive), then you would end up using more space, since hardlinks only work within a single disk. In that case, one may want to consider enabling softlinks instead (see conda config --describe allow_softlinks for details).

Keep in mind that this workaround completely depends on the current internal behavior of Conda to recognize all folders inside one of the envs_dirs directories as a nameable env.

merv
  • 67,214
  • 13
  • 180
  • 245
  • Thank you. I will try that. Thank you very much for clarifying things in so much detail. – Rich Lysakowski PhD Jul 06 '20 at 18:46
  • One question to clarify things for me for the future. Is the first line in the configuration environment used as the "default" location where any new environment will be created? In your example this would be the first line after the envs_dirs variable. " - /Users/user/miniconda3/envs" for the output of your command ```conda config --show envs_dirs``` which shows ```envs_dirs: - /Users/user/miniconda3/envs - /Users/user/.conda/envs``` – Rich Lysakowski PhD Jul 06 '20 at 23:32
  • @RichLysakowskiPhD yes, the first line behaves as the default – merv Jul 07 '20 at 00:32