0

I have a

~/.emacs.d/init.el

which includes:-

(load-file "blabla.el")

blabla.el is in ~/.emacs.d/

When I start emacs I get this error:-

File is missing: Cannot open load file, No such file or directory, /root/blabla.el

I can use a full path to load the file, but do I need to? I thought emacs would look in emacs home directory (~/.emacs.d/) to find files. What have I missed?

1 Answer 1

2

load-file needs an absolute file name. If you don't give it an absolute path, it will have to generate one based on the current buffer's default-directory which isn't going to be what you want:

$ cd /
$ emacs -Q --batch --eval "(load-file \"foo.el\")"
Cannot open load file: No such file or directory, /foo.el

$ cd /tmp
$ emacs -Q --batch --eval "(load-file \"foo.el\")"
Cannot open load file: No such file or directory, /tmp/foo.el

I thought emacs would look in emacs home directory (~/.emacs.d/) to find files.

You don't want that either. Emacs will not look in ~/.emacs.d by default when loading lisp files (and see Warning message about load-path. or https://stackoverflow.com/q/24779041 regarding the fact that Emacs will complain if you try to make it do so).

In code, you would usually use either (load "foo") or (require 'foo) to load a foo.el[c] library with Emacs looking for it in the directories listed in the load-path variable.

I suggest reading:

  • C-hig (emacs)Lisp Libraries
  • C-hig (elisp)How Programs Do Loading
  • C-hf require
4
  • I have added a lisp sub-directory, added it to the load-path and moved blabla.el to it. C-h v load-path shows ~/.emacs.d/lisp as the first directory. However when starting emacs I still get |File is missing: Cannot open load file, No such file or directory, /root/blabla.el What am I missing?
    – balanga
    Sep 9 at 6:21
  • I'm guessing you're still using load-file which hasn't changed its behaviour since I described it.
    – phils
    Sep 9 at 8:31
  • Oops. You're correct. Many thanks. I'm new to all this. I thought you needed to 'load-file' rather than 'load'.
    – balanga
    Sep 9 at 8:44
  • All of the functions for loading files ultimately call load, so anything that you can do with the other functions you can also do with load (depending on the arguments you pass). There's no harm in using one of the other functions if its behaviour is actually what you want, but in lisp code you can always use load directly.
    – phils
    Sep 9 at 10:08

Your Answer

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

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