Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conda set LD_LIBRARY_PATH for env only [duplicate]

I have an installation of miniconda3 where I have created a virtual environment called py35. I have some libraries that I only want to use from within this environment. hence they are under

 /.../miniconda3/envs/py35/libs 

However they are not found from within the environment as LD_LIBRARY_PATH does not contain said folder. I now want to set LD_LIBRARY_PATH to include the /lib only when I am in the virtual environment.

I was thinking of modifying the activate script miniconda uses to start the environment but am not quite sure if this is standard practice or if there is an easier way to achieve this.

like image 992
FlyingTeller Avatar asked Oct 19 '17 09:10

FlyingTeller


People also ask

How do I set environment variables in conda virtual environment?

To set environment variables, run conda env config vars set my_var=value . Once you have set an environment variable, you have to reactivate your environment: conda activate test-env . To check if the environment variable has been set, run echo $my_var ( echo %my_var% on Windows) or conda env config vars list .

How do I change my Anaconda environment prompt?

You can always use conda activate or conda deactivate to switch between your environments. You can directly activate the environment you wish to use by using conda activate . conda deactivate will deactivate your current active environment and change to the default environment which is the base environment.

What is LD_LIBRARY_PATH environment variable?

The LD_LIBRARY_PATH environment variable tells Linux applications, such as the JVM, where to find shared libraries when they are located in a different directory from the directory that is specified in the header section of the program.


2 Answers

You can set environment variables when an environment is activated by editing the activate.d/env_vars.sh script. See here: https://conda.io/docs/user-guide/tasks/manage-environments.html#macos-and-linux

The key portions from that link are:

  1. Locate the directory for the conda environment in your Terminal window, such as /home/jsmith/anaconda3/envs/analytics.

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

    cd /home/jsmith/anaconda3/envs/analytics 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.

like image 106
darthbith Avatar answered Sep 18 '22 14:09

darthbith


I just wanted to add that you could declare 2 variables in the activate.d/env_vars.sh like, it makes it easier to reset the variable to the pre-activation state:

export OLD_LD_LIBRARY_PATH=${LD_LIBRARY_PATH} export LD_LIBRARY_PATH=/your/path:${LD_LIBRARY_PATH} 

and then in deactivate.d/env_vars.sh:

export LD_LIBRARY_PATH=${OLD_LD_LIBRARY_PATH} unset OLD_LD_LIBRARY_PATH 
like image 20
Oleksandr Avatar answered Sep 17 '22 14:09

Oleksandr