I keep packages organized on my laptop using environment modules (http://modules.sourceforge.net/).

Hence if I need a particular version of python, I can do module load python39 etc.

Now if I first load all the required modules in a terminal, and then call emacs from the command line, emacs takes all environment variables appropriately and works great. But when I launch it from start menu, it complains that it cannot find python.

How can I make emacs load the required environment modules?

So when I open any python file in Emacs, I get following error

Output from Backend

There was some unexpected output from the Elpy backend. This is
usually not a problem and should usually not be reported as a bug with
Elpy. You can safely hide this buffer and ignore it. You can also see
the output below in case there is an actual problem.

/opt/python39/install/bin/python3: error while loading shared libraries: libpython3.9.so.1.0: cannot open shared object file: No such file or directory

the reason being that the path to libpython is defined in my python module.

I am using Ubuntu 20.04, Modules 5.0.1, Emacs 28.1

  • Please add some details about your machine's operating system and the precise error message you receive. I suspect that the issue you describe originates there, not in Emacs. Emacs does not depend on Python (which came into being many years after Emacs), so I suspect that whatever is failing to find Python is not Emacs. Commented Sep 7, 2022 at 21:40
  • Why can't you write a wrapper script that loads the environment modules and then starts Emacs, and make that script be what is invoked from the Start menu, instead of the bare Emacs?
    – NickD
    Commented Sep 7, 2022 at 22:05
  • 2
    Because I was not convinced that that would be best way forward. Every time I install a new module, I would need to edit the launch script. If somehow I can load it from Emacs itself, then I can load conflicting modules as well (e.g. load python 3.9 for one project and 3.8 for another). In case of launch script it would mean changing everytime before launching.
    – ipcamit
    Commented Sep 7, 2022 at 22:12

1 Answer 1


Fortunately for you, it seems that the modules system supports Emacs. On my system, the initialization file was located in /usr/share/modules/init/lisp. The documentation for how to use it with Emacs can be found in their git repo under modules/docs/emacs.txt. I'll repeat the basic instructions here for convenience.

    ;; load and init emacs lisp modules if not already loaded...
    (if (and (getenv "MODULESHOME")
             (file-directory-p (getenv "MODULESHOME"))
             (not (fboundp 'module)))
          (if (load-file (concat (getenv "MODULESHOME") "/init/lisp"))
              (define-key global-map "\C-cm" 'Modules-module))))

After this, from within your emacs process you can run any module command, eg:

  • C-c m load modules
  • C-c m list
  • 1
    Ah! worked perfectly! Thank you
    – ipcamit
    Commented Sep 8, 2022 at 2:27
  • @ipcamit This is a really nice system. I didn't know about it until you asked this question, but I've started using it myself.
    – g-gundam
    Commented Sep 17, 2022 at 21:04
  • Glad I could be of help! It is fairly common in academia, specially high performance computing, where users want 10 different versions of same compiler because come code was written in 1996 and is at the base of every other package. However I have found out that it is not the best way forward for me. As several of emacs package does not work properly, unless they get environment at runtime I think. So other than python, you mileage might vary :( (eg. cider could not detect clojure even after loading. Cider only works if i load module in shell, then launch emacs from there)
    – ipcamit
    Commented Sep 18, 2022 at 14:09

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.