8

Ok, this is a noob question but I've digging into this enough that I think it's reasonable to ask :)

My question: In one's .emacs / init.el file, can one use require to load packages that were installed via the emacs package manager? If so, how?

I've seen a lot of code that says (require 'package-name), where 'package-name is something like 'powerline or 'rainbow-delimiters. The docs say that require basically tries to load the file, making sure not to load it twice. load works by looking in all the dirs in the load-path variable. This makes sense.

package-initialize goes through the list of installed packages and tries to run their auto-loads (and possibly defer further work) so that all the packages I've previously installed will work. This makes sense.

My confusion comes from my init.el: even though I've got packages installed (I can both see the packages in the filesystem and everything works great if I run (package-initialize)) using require doesn't work. Emacs complains that use-package isn't in load-path and I can confirm that if I don't call (package-initialize) that the load-path doesn't ilst ~/.emacs.d/elpa (or anything under it).

My question: In one's .emacs / init.el file, can one use require to load packages that were installed via the emacs package manager? If so, how? Do I need to manually add the directory to load-path? Should this normally work and I probably broke something with my personal init.el?

Any help would be appreciated - I'm clearly not getting something about how require works (and many other things too, I'm sure :) )

EDIT: My version: GNU Emacs 26.1 (build 1, x86_64-w64-mingw32) of 2018-05-30

Also, I'm playing around with use-package and am running into the same problem - if I add the directory containing use-package to load path without running (package-initialize) first then use-package fails because it wants to use the package bind-key. bind-key is installed but not listed in load-path so emacs barfs at that point.

MikeTheTall
  • 659
  • 4
  • 11
  • 2
    For this question, Emacs version is very important. There are significant changes between Emacs 25, 26 and 27 (the development series). Mention the Emacs version you need this answer for. – Jeeves Aug 23 '18 at 09:22
  • 1
    Please start `emacs -Q`, run `M-x package-initialize`, and check `load-path` again. I have a similar system. After executing above steps I have many `load-path` entries with `elpa` in them. The option `-Q` essentially avoids loading the initialization files of the system and the user. – Tobias Aug 23 '18 at 16:16
  • @Tobias - I think my goal is the opposite of this - I'm wondering how to avoid doing `package-initialize` and yet still run my `init.el`. My understanding is that `use-package` can then lazy-load packages as I need them, thus reducing 'boot' times *and* giving me a snazzy, nice syntax for loading packages. – MikeTheTall Aug 23 '18 at 17:44
  • It is for error analysis not for permanent usage. – Tobias Aug 23 '18 at 18:06
  • Sure - sorry! when `emacs -Q` starts up `load-path` doesn't have any paths to my `elpa` dir. After I run `(package-initialize)` then `load-path` does have a bunch of `elpa` directories. Is that what you were looking for? (I don't understand what this tells you?) – MikeTheTall Aug 23 '18 at 18:29

1 Answers1

5

You should be able to use require, yes, but only after running package-initialize (in Emacs≥27, this is done for you before the beginning of the .emacs file).

Note that for 99% of the packages require should not be needed (the autoloads should cause the package to be loaded as soon as you try to make use of it). E.g. if you have use-package installed as an ELPA package, then you should be able to use (use-package ...) without first doing (require 'use-package).

But again, that's only true after having run package-initialize.

Stefan
  • 26,154
  • 3
  • 46
  • 84
  • Did you mean Emacs≥27? – npostavs Aug 24 '18 at 02:13
  • Can I ask what the point of `'require` is? (Is it left over from an era when it was useful?) – MikeTheTall Aug 25 '18 at 02:40
  • 1
    At this point, does that mean that `(use-package)` is mostly useful for configuring and auto-installing a given package? Other than the auto-installing and the nice syntax, what does `use-package` provide? (the web page for use-package talks a lot about lazy-loading packages - how can that happen if `package-initialize` has already loaded everything? – MikeTheTall Aug 25 '18 at 02:42
  • @MikeTheTall `require` is still very much used and useful - it's the canonical way to load libraries. What Stefan is saying is that most user configuration code seldom needs to eagerly load library code. – Basil Apr 05 '20 at 15:32
  • 1
    @MikeTheTall `package-initialize` does not eagerly load installed packages - it only adds their directories to `load-path` and evaluates their [autoloads](https://www.gnu.org/software/emacs/manual/html_node/emacs/Lisp-Libraries.html). – Basil Apr 05 '20 at 15:33
  • @MikeTheTall `use-package` is just a handy syntax for keeping various settings related to a single package grouped together and making it easier, especially for beginner Emacsites, to lazy-load packages by default. Ultimately, everything `use-package` can do, vanilla Emacs gives you ways of doing more flexibly, but many find `use-package` more convenient or more aesthetically pleasing. – Basil Apr 05 '20 at 15:37