I am trying to create virtual environment using environment.yml in miniconda (Where environment.yml contains a list of all dependecies.) using the following command:
conda env create -f environment.yml
but I get this error (this is entire output)
Error: prefix already exists: /home/danish/miniconda3/envs/venv
Can someone help me correcting the error?
Thanks in advance :)
The environment.yml specifies that the name of the environment is venv at the top of your file -- i.e.
name: venv
But that environment already exists (you can see it via conda env list). The solution here is to change the name in the environment.yml or use a different name when you are creating the environment. Example:
conda env create -f environment.yml -n new-env-name
Where the new-env-name is an environment name you haven't used yet.
Related
This question already has an answer here:
Where I can find conda yml keys?
(1 answer)
Closed 1 year ago.
To save a conda environment and re-create it, I use:
# Save the environment
conda env export > my_conda_env.yml
# Re-create the environment
conda env create --file my_conda_env.yml
# Reactivate the environment
conda activate pytorch
I notice that my_conda_env.yml contains prefix: /home/franck/anaconda3/envs/pytorch on the last line. What's the point of it?
It specifies the directory to put the environment in.
Straight from the documentation:
You can control where a conda environment lives by providing a path to
a target directory when creating the environment. For example, the
following command will create a new environment in a subdirectory of
the current working directory called envs:
conda create --prefix ./envs jupyterlab=0.35 matplotlib=3.1 numpy=1.16
Or, you could have checked:
conda env create --help
And it shows:
-p PATH, --prefix PATH
Full path to environment location (i.e. prefix).
I read that the prefix line in the environment.yaml file is not used by conda env create. Two of the posts on SO pointing to this fact are:
export conda environment without prefix variable which shows local path to executable
Anaconda export Environment file
I have the reverse problem of most of these posts
I want to specify inside the file the actual prefix, so that different users setup their environments in their home directory in a shared machine.
However, as previously mentioned the command for creating environments is completely ignoring the prefix line.
I managed to setup an environment to a specific path using a prefix like this:
conda env create --prefix=<prefix> --file=environment.yaml
but I am trying to figure a way to define the prefix so the user will not have to type it themselves but it will be automatically configured to be their home directory.
I work around the lacking of proper solution by using Makefile target
# project_root/Makefile
# from my experience mamba installs and updates faster than conda
# conda install mamba -n base -c conda-forge
env-create:
mamba env create -p ./envs -f environment.yml
env-update:
mamba env update -p ./envs -f environment.yml
usage:
$ make env-create
# or
$ make env-update
I have created an environment called B3 inside anaconda-navigator. It works fine if launched from within navigator.
However, when I want to activate it at the shell, I get 'could not find environmnet B3.'
If I use conda env list, the environment is visible but its name is blank. If I try using the file path instead, I get 'Not a conda environment.'
Why is the name missing, and how can I activate it from the shell?
Name-based reference of Conda environments only works for environments located in one of the directories listed in the envs_dirs configuration option (see conda config --describe envs_dirs). By default this corresponds to the envs/ subdirectory in the Conda installation. If you create an env outside of one of these directories, then you cannot use a name to reference it. Instead, one must activate it by its path:
Option 0: Activate by Path (Fix OP’s Typo)
conda activate /home/julianhatwell/anaconda3/envs/B3
Note that OP originally had a typo (anaconda2 should have been anaconda3). After pointing this out (see comments to question), the questioner instead requested an answer to:
“How to convert a nameless environment to named one?”
Converting to Named Environment
The following are possible ways to enabling name-based activation.
Option 1: Clone Into Directory
One option to use conda activate B3, is to recreate your B3 env in the default directory. You can use the --clone flag to accomplish this.
conda create --clone path/to/the/nameless_env -n named_env
Option 2: Add Parent Directory
Alternatively, you can add the parent directory of the environment in question to the envs_dirs configuration option.
conda config --append envs_dirs /path/to/the/parent_dir
Option 3: Symbolic Link
Another possibility is to create a symbolic link in one to the envs_dirs folders to the environment folder. It seems to work, but it is not a common practice, so it may have downsides that are unreported.
To get the list of the available environments use:
conda env list
To activate the nameless environment use:
conda activate <Folder>
When you create a conda env with --prefix, it will not have a name,
and to give one do the following:
# ex path: /Users/username/opt/miniconda3/envs/`
conda config --append envs_dirs <path to env folder here>
To activate the environment:
conda activate <name of the env>
Faced a similar issue on Apple M1 chip due to installation of miniforge3 and miniconda in two different paths.
My solution Edit the .bash_profile
It is most likely that you have ps1 value set to False, which enables prompt change with change of conda environment.
To check run from your ubuntu terminal:
$ conda config --show | grep changeps1
And set it to True using:
$ conda config --set changeps1 True
After this, you should see the currently activated conda environment name at the beginning of each prompt. PS - You may have to close and reopen the terminal for this to take effect.
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
I want to import an existing virtual env, which I created using anaconda into another anaconda installation on a different distribution.
I've tried creating a new one using the following command in the directory of the venv copied from the other distribution:
conda create -p . python=3.4
That results in:
Error: prefix already exists: /home/xiaolong/development/blog
But anaconda does not know that, when I ask it to list all existing venvs:
conda info --envs
This results in:
# conda environments:
#
firstenv /home/xiaolong/development/anaconda3/envs/firstenv
gtkplus-tool /home/xiaolong/development/anaconda3/envs/gtkplus-tool
testenv /home/xiaolong/development/anaconda3/envs/testenv
tkxld /home/xiaolong/development/anaconda3/envs/tkxld
wxpython-phoenix-tutorial /home/xiaolong/development/anaconda3/envs/wxpython-phoenix-tutorial
root * /home/xiaolong/development/anaconda3
This list is missing my copied venv. How do I add it to that list, so that I can then use source activate blog for example?
-p . cannot work because the directory must not exist already.
You probably want your env in /home/xiaolong/development/blog/env or something similar. So just do conda create -p ./env python=3.4.