Search code examples
condaconda-build

How to set environment variables in a conda package so they are set when an environment containing that package is activated?


I know I can create an env_vars.(bat|sh) inside the activate.d directory in an environment, however I want the variables to be included as part of a package, so if the package is swapped out to a different version, it will change the environment variables.

According to the documentation here: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#saving-environment-variables, I should be able to create an env_vars.(bat|sh) as part of a conda package

This type of script file can be part of a conda package, in which case these environment variables become active when an environment containing that package is activated.

You can name these scripts anything you like. However, multiple packages may create script files, so be sure to use descriptive names that are not used by other packages. One popular option is to give the script a name in the form packagename-scriptname.sh, or on Windows, packagename-scriptname.bat.

I've tried creating both env_vars.bat and packagename-env_vars.bat which then set environment variables as described in the docs, but installing the package and activating the environment does not create the variables. Is there another step I need to do?

My meta.yml:

package:
  name: maya
  version: 2020

My env_vars.bat:

set MAYA_VERSION=2020
set MAYA_LOCATION="C:\Program Files\Autodesk\Maya%MAYA_VERSION%"

Solution

  • As cel mentioned, the env_vars needs to copied into the activate.d folder. I did not know enough about conda to know that when the docs say a package can contain those scripts, that those scripts actually have to be copied over, there is no automatic running of the scripts.

    Actually there are two env_var.bat files: one to set the variables on environment activation, and another to unset the variables when the environment is deactivated.

    maya-activate-env_vars.bat

    @echo off
    set MAYA_VERSION=2020
    set MAYA_LOCATION="C:\Program Files\Autodesk\Maya%MAYA_VERSION%"
    

    maya-deactivate-env_vars.bat

    @echo off
    set MAYA_VERSION=
    set MAYA_LOCATION=
    

    The piece that was missing was the bld.bat script that copied the *env_vars.bat files to the proper (de)activate.d directories when the package is installed:

    bld.bat

    setlocal EnableDelayedExpansion
    for %%F in (activate deactivate) DO (
        if not exist %PREFIX%\etc\conda\%%F.d mkdir %PREFIX%\etc\conda\%%F.d
        copy %RECIPE_DIR%\maya-%%F-env_vars.bat %PREFIX%\etc\conda\%%F.d\%PKG_NAME%-%%F-env_vars.bat
    )
    

    It should be noted that if you remove the package and then deactivate the environment, the variables will remain because the env_vars in deactivate.d will have been removed so the variables are never unset. You can fix this my removing the package after the environment has been deactivated with -n flag.