1

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%"
Chad Vernon
  • 593
  • 4
  • 12
  • Did you copy `ènv_vars.bat` to the etc folder as described in the docs? Activate scripts need to be copied to the `%CONDA_PREFIX%\etc\conda\activate.d` folder, otherwise they will not work. – cel Apr 15 '20 at 20:21
  • Do I need to manually do that? Is there an automated way to do it when the package is installed and to remove them when the package is removed? – Chad Vernon Apr 15 '20 at 20:39
  • I see there are activate scripts which could copy the env_vars: https://docs.conda.io/projects/conda-build/en/latest/resources/activate-scripts.html Although it does say: "It is generally recommended to avoid using activate scripts when another option is possible" What other options are there? – Chad Vernon Apr 15 '20 at 20:46

1 Answers1

1

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.

Chad Vernon
  • 593
  • 4
  • 12