I would like to set the environment variables defined within Emacs available to inferior shells launched afterwards.

My use-case is to be able to switch Python (conda) environment within Emacs.

If I do:

(setenv "CONDA_DEFAULT_ENV" "py_39")

and launch an inferior shell with C-u M-x shelland type echo $CONDA_DEFAULT_ENV, I still have base.

According to this answer, setenv should set environment variables for the inferior shell launched afterwards. It does not work for me.

FWIW: I am running Emacs 30.0.50

  • Works for me, on a just-built Emacs with the same version (I didn't realize that the version was bumped).
    – NickD
    Dec 6, 2022 at 13:21
  • 3
    Probably a shell initialization file is invoked that sets the CONDA_DEFAULT_ENV variable after Emacs launches the shell. Try setting a nonsense variable (unlikely to be found in one of your shell init files): (setenv "FOO" "BAR1234") launch an inferior shell and check: I bet it will work. So find the place in your shell init file(s) that sets CONDA_DEFAULT_ENV: if it is in your .bashrc (or equivalent), then move it to the appropriate .profile file. Your .bashrc is sourced when you start any interactive shell, so it probably clobbers the setting in your Emacs environment.
    – NickD
    Dec 6, 2022 at 15:02
  • you are right ! The problem is due to ~/.zshrc and some code ran by conda when the shell got initialised
    – ldc
    Dec 7, 2022 at 8:52

1 Answer 1


As pointed out by @NickD in a comment above the environment variable is correctly set but is then unset by the shell init script (~/.zshrc in my case).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.