linux 环境变量管理系列(1):"sudo", "source activate env_name"

sudo 如何保持 PATH?

  • 问题:sudo 后 PATH 后改变
    (action) fanglin@lab704-SYS-7048GR-TR:~$ python -V
    Python 3.6.5 :: Anaconda, Inc.
    (action) fanglin@lab704-SYS-7048GR-TR:~$ sudo python -V
    Python 2.7.12
    
  • 解决方法:将 sudo command 改为 sudo env "PATH=$PATH" command 即可保持 PATH,来源:stackoverflow.com: Why does sudo change the PATH?

source activate 如何自动更新环境变量

  • 问题: 运行 source activate env_name 之后再运行 python -V 仍然是旧环境中的 python

  • 解决办法:

    1. Locate the directory for the conda environment in your terminal window by running in the terminal echo $CONDA_PREFIX.

    2. Enter that directory and create these subdirectories and files:

      cd $CONDA_PREFIX
      mkdir -p ./etc/conda/activate.d
      mkdir -p ./etc/conda/deactivate.d
      touch ./etc/conda/activate.d/env_vars.sh
      touch ./etc/conda/deactivate.d/env_vars.sh
      
    3. Edit ./etc/conda/activate.d/env_vars.sh as follows:

      #!/bin/sh
      
      export MY_KEY='secret-key-value'
      export MY_FILE=/path/to/my/file/
      
    4. Edit ./etc/conda/deactivate.d/env_vars.sh as follows:

      #!/bin/sh
      
      unset MY_KEY
      unset MY_FILE
      

    When you run conda activate analytics, the environment variables MY_KEY and MY_FILE are set to the values you wrote into the file. When you run conda deactivate, those variables are erased.

  • 来源:

    1. manage-environments
    2. Anaconda配置特定环境的环境变量

你可能感兴趣的:(linux 环境变量管理系列(1):"sudo", "source activate env_name")