How to share an Anaconda Python environment between multiple users? - python

I have Anaconda 5.1 Python distribution installed (by the system admin) on Windows 10, for all users. I can create an environment and then view the available environments:
λ conda create --name py35 python=3.5 anaconda
...
λ conda env list
# conda environments:
#
base * C:\ProgramData\Anaconda3
py35 C:\Users\<my-user-name>\AppData\Local\conda\conda\envs\py35
When I log in as a different user, however, only the base environment is visible/available. How can I create an environment and make it available to all users of the system?
The documentation discusses multi-user installations, but I cannot see how to make environments available to other users.

I would shy away from sharing environments with other users, because if they don't know what they are doing, they could add packages that could conflict with other packages and/or even delete packages that another user might need. The preferred approach is that after you have created an environment, you export it as a yml file:
conda env export > environment.yml
Then you send the users the yml file and have them build their own environment using the yml:
conda env create -f environment.yml
If you really want to use a shared environment where every user can access, then you have to use the -p or --prefix option in your create:
conda create -p C:/full/public/path/to/py35 python=3.5
And then instruct your users to add the public path (C:/full/public/path/to) to their conda config file. Then, they should be able to see the environment when running conda env list.

The key here is adding the path to the folder containing the environment(s) to the user's conda configuration file .condarc. Like this:
envs_dirs:
- C:\full\path\to\environments\folder
This makes all the environments (subfolders within) available to the user. It does not appear to be possible to make a specific, named environment available.
As has been pointed out, you can create an environment in a specific location using the -p flag, and then add the parent directory to the configuration file, but this is not a requirement. This may be useful, however, to avoid permissions errors if sharing environments that exists in protected user areas.
On Windows 10, my user configuration file was in C:\Users\<my-user-name>\, and I just added the above text to the end of it.

The Conda-Cheatseet here lists these following commands.
Save Environment:
conda list --explicit > my-env.txt
Create Environment:
conda env create --file my-env.txt

Related

How to create and activate conda env in one line?

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.

Why can not environment created by Anaconda be used in Cmder?

I used win10 64bit. The problem is that conda seems can not find env name, only env location.
*********************in system cmd*********************
#conda env list
# conda environments:
#
base C:\Users\czk\Anaconda3
py37 * C:\Users\czk\Anaconda3\envs\py37
testnumpy C:\Users\czk\Anaconda3\envs\testnumpy
*********************in Cmder*********************
λ conda env list
# conda environments:
#
base * C:\ProgramData\Anaconda3
C:\Users\czk\Anaconda3\envs\py37
C:\Users\czk\Anaconda3\envs\testnumpy
λ conda activate py37
Could not find conda environment: py37
You can list all discoverable environments with `conda info --envs`.
Above is the output in system cmd and Cmder. Also tried this answer, and without progress.
I also try and failed your answer (also tried comments).
However, here is a workaround:
conda activate C:\Users\<YOUR_USER>\.conda\envs\<YOUR_ENV>
You are using two different installations of anaconda, one in each tool ('system cmd' and 'Cmdr'). You can know this by seeing the path for the base environment is different from each. System cmd points to anaconda in your user folder (C:\Users\czk\Anaconda3), and Cmdr is pointing to the ProgramData folder (C:\ProgramData\Anaconda3).
Here's how you can ensure that all the environments can be activated by name, regardless which installation or console you're using:
Create a .condarc file in your user home directory (if you haven't already).
Add envs_dirs to it, like below. This tells conda where to look up environments by name. See docs on .condarc specification.
envs_dirs:
- C:\ProgramData\Anaconda3\envs
- C:\Users\czk\Anaconda3\envs

Conda create env from file - How to specify prefix in the file?

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

conda environment has no name visible in conda env list - how do I activate it at the shell?

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.

Single Anaconda Environment On Multiple Machines

Is it possible to share one environment between multiple machines? I regularly switch between machines, and would like to use a single anaconda environment.
Yes, if you have file access between the machine's you can load the remote env on the host machine with annaconda on the local machine. Or you can copy the annaconda env folder and manually transfer it between machines.
For detailed instructions refer to:
https://conda.io/docs/test-drive.html#managing-environments
Here is a scenario via the .condarc file and Anaconda PowerShell:
Suppose you are on machine_01, and you have an environment myenv on network-shared folder L:\python_projects\envs
To access the same environment myenv from another machine (machine_02) you have to do the following on machine_02:
1- Download the latest Anaconda from https://www.anaconda.com/ and finish installation on recommended path under your username (C:\Users\yourname\Anaconda3)
Note: you don’t need the admin rights because the installation will be under your username.
2- Check if Anaconda added to your path. Open Anaconda PowerShell from windows start menu and enter this command conda --version
The result look like: conda 4.12.0. Which means Anaconda added to path and everything is fine.
If you get a command not recognized error, type this command's where conda to find where is conda installed then add the conda path to your PATH in Windows Environment Variables.
3- Enter this command conda info --envs into your Anaconda PowerShell to check the available environments. You will find the base environment only like this:
# conda environments:
#
base * C:\Users\yourname\Anaconda3
4- Open the .condarc file under C:\Users\yourname on machine_02 and add the path of environment's shared folder at the end of the file:
envs_dirs:
- L:\python_projects\envs
5- Save the .condarc file and type this command again conda info --envs to list the available environments. You will find the new environment with the base environment:
# conda environments:
#
base * C:\Users\yourname\Anaconda3
myenv L:\ python_projects\envs\myenv

Categories

Resources