127

I'd like the root environment of conda to copy all of the packages in another environment. How can this be done?

2
  • 3
    Do you mean "... copy all of the packages from another environment" [into root]?
    – pylang
    Commented Nov 20, 2016 at 10:26
  • 33
    conda create --name venv2 --clone venv1 Commented Jul 26, 2022 at 10:03

4 Answers 4

181

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.

5
  • 2
    This technique seems to only work for packages that were installed with conda or pip, right? Some packages that were not installed with simple conda or pip package managers don't work. For example Tensorflow, which was build using Bazel, does not copy over to a new environment via yaml files.
    – mikal94305
    Commented Nov 22, 2016 at 6:59
  • To overcome the above comment, can one via a bash script copy over environment directory files into the root directory?
    – mikal94305
    Commented Nov 22, 2016 at 9:50
  • I know this works for conda/pip packages. I am unfamiliar with the setup you described.
    – pylang
    Commented Nov 22, 2016 at 13:47
  • This answers the original question you posted. Your comments reflect another problem. Perhaps you should edit your question.
    – pylang
    Commented Nov 22, 2016 at 19:18
  • With version 4.4.8, the first line gives >CondaEnvException: Conda Env Exception: Unable to determine environment
    – abalter
    Commented Feb 16, 2018 at 6:42
100

To make a copy of your root environment (named base), you can use following command; worked for me with Anaconda3-5.0.1:

conda create --name <env_name> --clone base

you can list all the packages installed in conda environment with following command

conda list -n <env_name>
1
  • 1
    Using this method doesn't install packages in the new environment when installing with anaconda prompt i keeps checking into the cloned environment any solution for that?
    – DevPy
    Commented Sep 27, 2020 at 15:25
14

When setting up a new environment and I need the packages from the base environment in my new one (which is often the case) I am building in the prompt a identical conda environment by using a spec-file.txt with:

conda list --explicit > spec-file.txt

The spec-file includes the packages of for example the base environment.

Then using the prompt I install the the packages into the new environment:

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

The packages from base are then available in the new environment.

The whole process is describe in the doc: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#building-identical-conda-environments

2
  • 2
    This does not copy pip installed packages. Worth noting I think.
    – LuTze
    Commented Mar 24, 2022 at 15:59
  • This is worth noting. But Spyder says: Don't mix pip and conda youtube.com/watch?v=Ul79ihg41Rs Could be wise in general. Commented Mar 26, 2022 at 10:47
11

I also ran into the trouble of cloning an environment onto another machine and wanted to provide an answer. The key issue I had was addressing errors when the current environment contains development packages which cannot be obtained directly from conda install or pip install. For these cases I highly recommend conda-pack (see this answer):

pip install conda-pack

or,

conda install conda-pack

then back up the environment, to use the current environment just omit the my_env name,

# Pack environment my_env into my_env.tar.gz
$ conda pack -n my_env

# Pack environment my_env into out_name.tar.gz
$ conda pack -n my_env -o out_name.tar.gz

# Pack environment located at an explicit path into my_env.tar.gz
$ conda pack -p /explicit/path/to/my_env

and restoring,

# Unpack environment into directory `my_env`
$ mkdir -p my_env
$ tar -xzf my_env.tar.gz -C my_env

# Use Python without activating or fixing the prefixes. Most Python
# libraries will work fine, but things that require prefix cleanups
# will fail.
$ ./my_env/bin/python

# Activate the environment. This adds `my_env/bin` to your path
$ source my_env/bin/activate

# Run Python from in the environment
(my_env) $ python

# Cleanup prefixes from in the active environment.
# Note that this command can also be run without activating the environment
# as long as some version of Python is already installed on the machine.
(my_env) $ conda-unpack

Not the answer you're looking for? Browse other questions tagged or ask your own question.