How to determine whether a package is installed in elisp?

Vivodo picture Vivodo · Oct 17, 2011 · Viewed 10.1k times · Source

I want to customize environment while the specific package is installed properly. How to check whether some package is installed in elisp? Something like this?:

(if (require 'ecb)
    (progn (setq ....))
  (message "ECB not installed!"))

Answer

phils picture phils · Oct 17, 2011

tripleee's answer is a handy example of error handling, but unnecessary in this instance.

(when (require 'some-library nil 'noerror)
  do-things)

That 'noerror can be any non-nil value, but of course it's more descriptive this way. I often see :noerror used as well, but I've no idea if there's any particular advantage to using a keyword argument over a symbol (comments, anyone? I'm quite interested to know).

require is a built-in function in C source code.

(require FEATURE &optional FILENAME NOERROR)

If feature FEATURE is not loaded, load it from FILENAME.
If FEATURE is not a member of the list features, then the feature
is not loaded; so load the file FILENAME.
If FILENAME is omitted, the printname of FEATURE is used as the file name,
and load will try to load this name appended with the suffix .elc or
.el, in that order. The name without appended suffix will not be used.
See get-load-suffixes for the complete list of suffixes.
If the optional third argument NOERROR is non-nil,
then return nil if the file is not found instead of signaling an error.
Normally the return value is FEATURE.
The normal messages at start and end of loading FILENAME are suppressed.