17
votes

I'm creating a Django web app and using Conda to manage the required packages and virtual environment. I want to set an environmental variable (the Django settings location) as soon as I activate the environment. It should also change the current directory to my project directory.

I previously used virtualenvwrapper and I just added the commands to the postactivate script.

How can I achieve this using Conda?

1
I tried to activate the environment through a script but for some reason it doesn't work. For now I have to activate the environment and then run my script manually. - Kritz
I've found a workaround. I add the conda activate command and all the other commands as an alias in .bash_aliases. I only have 3 extra commands so it works well. - Kritz
I propose consolidate this question as a duplicate of this earlier version: How to set specific environment variables when activating conda environment? - merv

1 Answers

28
votes

That feature has been available since conda v3.8 (after this question was asked). Any *.sh files placed in ${CONDA_PREFIX}/etc/conda/activate.d/ will be sourced when the environment is activated.

Docs: https://conda.io/docs/user-guide/tasks/manage-environments.html#saving-environment-variables

Also explained here: https://groups.google.com/a/continuum.io/d/msg/anaconda/sQJQ7DBwpfI/pP4_Esbl6VYJ