2020年9月25日 星期五

[ Python 常見問題 ] How to share an Anaconda Python environment between multiple users?

 Source From Here

Question
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.

How-To
(Installation of conda) 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. (doc - sharing an environment) For example:
$ conda env list
# conda environments:
#
base * /opt/anaconda3


$ mkdir /conda_env/

// -p PATH, --prefix PATH: Full path to environment location (i.e. prefix).
$ conda create -p /conda_env/my_env python=python3.7
$ conda activate /conda_env/my_env/
(/conda_env/my_env) $ conda install numpy
(/conda_env/my_env) $ python -c "import numpy as np; print(np.__version__)"
1.19.1

(/conda_env/my_env) $ conda env export > environment.yml
(/conda_env/my_env) $ cat environment.yml
  1. name: /conda_env/my_env  
  2. channels:  
  3.   - defaults  
  4. dependencies:  
  5.   - _libgcc_mutex=0.1=main  
  6.   - blas=1.0=mkl  
  7.   - ca-certificates=2020.7.22=0  
  8.   - certifi=2020.6.20=py38_0  
  9.   - intel-openmp=2020.2=254  
  10.   - ld_impl_linux-64=2.33.1=h53a641e_7  
  11.   - libedit=3.1.20191231=h14c3975_1  
  12.   - libffi=3.3=he6710b0_2  
  13.   - libgcc-ng=9.1.0=hdf63c60_0  
  14.   - libstdcxx-ng=9.1.0=hdf63c60_0  
  15.   - mkl=2020.2=256  
  16.   - mkl-service=2.3.0=py38he904b0f_0  
  17.   - mkl_fft=1.2.0=py38h23d657b_0  
  18.   - mkl_random=1.1.1=py38h0573a6f_0  
  19.   - ncurses=6.2=he6710b0_1  
  20.   - numpy=1.19.1=py38hbc911f0_0  
  21.   - numpy-base=1.19.1=py38hfa32c7d_0  
  22.   - openssl=1.1.1h=h7b6447c_0  
  23.   - pip=20.2.2=py38_0  
  24.   - python=3.8.5=h7579374_1  
  25.   - readline=8.0=h7b6447c_0  
  26.   - setuptools=49.6.0=py38_0  
  27.   - six=1.15.0=py_0  
  28.   - sqlite=3.33.0=h62c20be_0  
  29.   - tk=8.6.10=hbc83047_0  
  30.   - wheel=0.35.1=py_0  
  31.   - xz=5.2.5=h7b6447c_0  
  32.   - zlib=1.2.11=h7b6447c_3  
  33. prefix: /conda_env/my_env  
// Exit conda virtual environment
(/conda_env/my_env) $ conda deactivate

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, we have to make sure the user has proper right to access the created conda virtual environment:
// Create a group for conda virtual environment
# groupadd conda_user

// Change the permission of target folder to be accessible by the created group
# chown root:conda_user -R /conda_env/my_env/
# chmod -R 775 /conda_env/my_env/

// Add the target user into created group
# usermod -a -G conda_user john

Then let's switch to another user `john` to make sure he can use our created conda virtual environment:
// Now we are using account john
// Let's activate the conda virtual environment
$ conda activate /conda_env/my_env/

// Confirm the numpy version is the same as the installed one previously
(/conda_env/my_env) $ python -c "import numpy as np; print(np.__version__)"
1.19.1

// Let's install another package coloredlogs
(/conda_env/my_env) $ python -c "import numpy as np; print(np.__version__)"
(/conda_env/my_env) $ python -c "import coloredlogs; print(coloredlogs.__version__)"
14.0
// Exit conda virtualenv
(/conda_env/my_env) $ conda deactivate

So far so good, let's use first account root to verify that the package installed by john account can be used by root too:
// Switch to account root and activate conda virtual environment
# conda activate /conda_env/my_env/
(/conda_env/my_env) # python -c "import coloredlogs; print(coloredlogs.__version__)"
14.0

Great! Everything works.

Supplement
FAQ - how to specify new environment location for conda create

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...