How to Move .Conda from One Folder to Another at the Moment of Creating the Environment

How to move .conda from one folder to another at the moment of creating the environment

Configure Environment and Package Default Locations

I'd guess that, despite your efforts to put your environments on the large partition, there is still a default user-level package cache and that is filling up the home partition. At minimum, set up a new package cache and a default environments directory on the large partition:

# create a new pkgs_dirs (wherever, doesn't have to be hidden)
mkdir -p /big_partition/users/user/.conda/pkgs

# add it to Conda as your default
conda config --add pkgs_dirs /big_partition/users/user/.conda/pkgs

# create a new envs_dirs (again wherever)
mkdir -p /big_partition/users/user/.conda/envs

# add it to Conda as your default
conda config --add envs_dirs /big_partition/users/user/.conda/envs

Now you don't have to fuss around with using the --prefix flag any more - your named environments (conda create -n foo) will by default be created inside this directory and you can activate by name instead of directory (conda activate foo).

Transferring Previous Environments and Package Cache

Unfortunately, there's not a great way to move Conda environments across filesystems without destroying the hardlinks. Instead, you'll need to recreate your environments. Since you may or may not want to bother with this, I'm only going to outline it. I can elaborate if needed.

  1. Archive environments. Use conda env export -n foo > foo.yaml (One per environment.)
  2. Move package cache. Copy contents of old package cache (/home/users/user_name/.conda/envs/.pkgs/) to new package cache.
  3. Recreate environments. Use conda env create -n foo -f foo.yaml.

Again, you could just skip this altogether. This is mainly if you want to be very thorough about transferring and not having to redownload stuff for environments you already created.

After this you can delete some the stuff under the old ~/.conda/envs/pkgs folder.

anaconda .conda folder move from /home/usrxy to some other location

you can use --prefix option documentation

Option 1:
If you want to create your virtual environment in current directory then use

conda create --prefix=envName python=X.X

Option 2: if you want to mention the directory then give full path

conda create --prefix=/YourPath/yourEnvName python=x.x

Option 3: If you dont want to explicitly mention the path everytime and want all your environments to be stored somewhere else by default, you can set that up in your .condarc file documentation

You can do this in command line using:

conda config --add envs_dirs <path to directory>

envs_dirs in your .condarc file will add an additional location to the package cache search path.

How can you clone a conda environment into the root environment?

There are options to copy dependency names/urls/versions to files.

Recommendation

Normally it is safer to work from a new environment rather than changing root. However, consider backing up your existing environments before attempting changes. Verify the desired outcome by testing these commands in a demo environment. To backup your root env for example:

λ conda activate root
λ conda env export > environment_root.yml
λ conda list --explicit > spec_file_root.txt

Options

Option 1 - YAML file

Within the second environment (e.g. myenv), export names+ to a yaml file:

λ activate myenv
λ conda env export > environment.yml

then update the first environment+ (e.g. root) with the yaml file:

λ conda env update --name root --file environment.yml     

Option 2 - Cloning an environment

Use the --clone flag to clone environments (see @DevC's post):

λ conda create --name myclone --clone root

This basically creates a direct copy of an environment.


Option 3 - Spec file

Make a spec-file++ to append dependencies from an env (see @Ormetrom):

λ activate myenv
λ conda list --explicit > spec_file.txt
λ conda install --name root --file spec_file.txt

Alternatively, replicate a new environment (recommended):

λ conda create --name myenv2 --file spec_file.txt

See Also

  • conda env for more details on the env sub-commands.
  • Anaconada Navigator desktop program for a more graphical experience.
  • Docs on updated commands. With older conda versions use activate (Windows) and source activate (Linux/Mac OS). Newer versions of conda can use conda activate (this may require some setup with your shell configuration via conda init).
  • Discussion on keeping conda env

Extras

There appears to be an undocumented conda run option to help execute commands in specific environments.

# New command
λ conda run --name myenv conda list --explicit > spec_file.txt

The latter command is effective at running commands in environments without the activation/deactivation steps. See the equivalent command below:

# Equivalent
λ activate myenv
λ conda list --explicit > spec_file.txt
λ deactivate

Note, this is likely an experimental feature, so this may not be appropriate in production until official adoption into the public API.

+Conda docs have changed since the original post; links updated.
++Spec-files only work with environments created on the same OS. Unlike the first two options, spec-files only capture links to conda dependencies; pip dependencies are not included.

How can I rename a conda environment?

New answer:

From Conda 4.14 you will be able to use just:

conda rename -n old_name -d new_name 

Although, under the hood, conda rename still uses [1][2] undermentioned combination of conda create and conda remove.



Old answer:

You can't.

One workaround is to create clone a new environment and then remove the original one.

First, remember to deactivate your current environment. You can do this with the commands:

  • deactivate on Windows or
  • source deactivate on macOS/Linux.

Then:

conda create --name new_name --clone old_name
conda remove --name old_name --all # or its alias: `conda env remove --name old_name`

Notice there are several drawbacks of this method:

  1. It redownloads packages (you can use --offline flag to disable it)
  2. Time consumed on copying environment's files
  3. Temporary double disk usage

There is an open issue requesting this feature.

Modiyfing conda configuration file does not reflect changes in environment

Background: Conda's configuration priorities

As documented in "The Conda Configuration Engine for Power Users" post, Conda sources configuration values from four sources, listed from lowest to highest priority:

  1. Default values in the Python code
  2. .condarc configuration files (system < user < environment < working directory)
  3. Environment variables (CONDA_* variables)
  4. Command-line specifications

Problem: Environment variable prioritized

We can observe how this plays out in OP's case, with the --show-sources result. Specifically, there are three places where envs_dirs is defined:

  1. System level configuration file at /util/opt/anaconda/4.9.2/.condarc
  2. User-level configuration file at /home/helikarlab/joshl/.condarc
  3. Environment variable CONDA_ENVS_PATH1

And since the environment variable takes priority and defines the preferred directory to be /home/helikarlab/joshl/.conda/envs, that will take precedence no matter what is set with conda config and .condarc files.

Workarounds

All the following workarounds involve manipulating the environment variable. It is unclear when the variable is set (probably via a system-level shell configuration file). It should be reliable to manipulate the variable by appending any of the following workarounds to user-level shell configuration file (e.g., ~/.bashrc, ~/.bash_profile, ~/.zshrc).

Option 1: Unset variable

One could completely remove the variable with

unset CONDA_ENVS_PATH

This would then allow the user-level .condarc to take priority.

However, this variable also appears to provide locations for several system-level shared environments. It is unclear how integral these shared environments are for normal functionality. So, removing the variable altogether could have additional consequences.

Option 2: Replace value

Conveniently, the location default and desired locations differ only by replacing /home with /work. This could be changed directly in the variable with:

export CONDA_ENVS_PATH=${CONDA_ENVS_PATH/\/home/\/work}

Option 3: Prepend desired default

The most general override would be to prepend the desired default path to the environment variable:

export CONDA_ENVS_PATH="/work/helikarlab/joshl/.conda/envs/:${CONDA_ENVS_PATH}"

This is probably the most robust, since it assumes nothing about the inherited value.

Additional Note

Users with small disk quotas in default locations should also consider moving the package cache (pkgs_dirs) to coordinate with the environments directory. Details in this answer.


[1]: CONDA_ENVS_DIRS and CONDA_ENVS_PATH are interchangeable, however only one can be defined at time. The former is the contemporary usage, so I believe the latter is likely supported for backward compatibility.

How to create conda env with both name and path specified

create a folder wherever you want to keep you environment files, inside the folder run:

conda create --prefix=yourenvname

When you wish to use this env, move to the folder in which you ran the previous command and do:

source activate yourenvname

Or

You can run:

conda create --prefix=path/yourenvname 

This will create environment named "yourenvname" in the specified path.



Related Topics



Leave a reply



Submit