18

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?

Kritz
  • 7,099
  • 12
  • 43
  • 73
  • 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 Jan 11 '16 at 06:07
  • 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 Jan 13 '16 at 06:08
  • 2
    I propose consolidate this question as a duplicate of this earlier version: [How to set specific environment variables when activating conda environment?](https://stackoverflow.com/questions/31598963/how-to-set-specific-environment-variables-when-activating-conda-environment) – merv Dec 10 '20 at 22:52
  • 1
    @merv that question is a different one and not a duplicate, even though the answers are very similar. Conda documentation only talks about setting env vars, which is done by placing them in some files in `activate.d`. There is not the same as placing a general script there and knowing it will be executed. This was misleading to me, and the answers there barely mention this. – Neinstein Sep 07 '21 at 14:27

1 Answers1

35

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/anaconda.com/g/anaconda/c/sQJQ7DBwpfI/m/pP4_Esbl6VYJ

Stuart Berg
  • 17,026
  • 12
  • 67
  • 99