emacsconfigurationmelpa

emacs startup error when modifying .emacs


I am new to emacs. I am trying to set it up for Python, using this guide. One of the first steps it recommends is adding the following to init.el:

;; init.el --- Emacs configuration

;; INSTALL PACKAGES
;; --------------------------------------

(require 'package)

(add-to-list 'package-archives
       '("melpa" . "http://melpa.org/packages/") t)

(package-initialize)
(when (not package-archive-contents)
  (package-refresh-contents))

(defvar myPackages
  '(better-defaults
    material-theme))

(mapc #'(lambda (package)
    (unless (package-installed-p package)
      (package-install package)))
      myPackages)

;; BASIC CUSTOMIZATION
;; --------------------------------------

(setq inhibit-startup-message t) ;; hide the startup message
(load-theme 'material t) ;; load material theme
(global-linum-mode t) ;; enable line numbers globally

;; init.el ends here

Since I had a .emacs file, I tried just appending it that file. Afterwards, my .emacs looked like:

(require 'package)
(setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
                         ("marmalade" . "http://marmalade-repo.org/packages/")
                         ("melpa" . "http://melpa.milkbox.net/packages/")))
(package-initialize)
(when (not package-archive-contents) (package-refresh-contents))

(let ((default-directory "~/.emacs.d/elpa/"))
    (normal-top-level-add-to-load-path '("."))
    (normal-top-level-add-subdirs-to-load-path))
(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )

;; init.el --- Emacs configuration

;; INSTALL PACKAGES
;; --------------------------------------



(add-to-list 'package-archives
       '("melpa" . "http://melpa.org/packages/") t)

(package-initialize)
(when (not package-archive-contents)
  (package-refresh-contents))

(defvar myPackages
  '(better-defaults
    material-theme))

(mapc #'(lambda (package)
    (unless (package-installed-p package)
      (package-install package)))
      myPackages)

;; BASIC CUSTOMIZATION
;; --------------------------------------

(setq inhibit-startup-message t) ;; hide the startup message
(load-theme 'material t) ;; load material theme
(global-linum-mode t) ;; enable line numbers globally

;; init.el ends here

However, this throws the error:

Warning (initialization): An error occurred while loading ‘/Users/adamg/.emacs’:

File error: http://melpa.org/packages/better-defaults-20160601.1219.el, Not found

To ensure normal operation, you should investigate and remove the
cause of the error in your initialization file.  Start Emacs with
the ‘--debug-init’ option to view a complete error backtrace.

Running emacs --debug-init returns:

Debugger entered--Lisp error: (file-error "http://melpa.org/packages/better-defaults-20160601.1219.el" "Not found")
  signal(file-error ("http://melpa.org/packages/better-defaults-20160601.1219.el" "Not found"))
  package-install-from-archive(#s(package-desc :name better-defaults :version (20160601 1219) :summary "Fixing weird quirks and poor defaults" :reqs nil :kind single :archive "melpa" :dir nil :extras ((:url . "https://github.com/technomancy/better-defaults") (:keywords "convenience")) :signed nil))
  mapc(package-install-from-archive (#s(package-desc :name better-defaults :version (20160601 1219) :summary "Fixing weird quirks and poor defaults" :reqs nil :kind single :archive "melpa" :dir nil :extras ((:url . "https://github.com/technomancy/better-defaults") (:keywords "convenience")) :signed nil)))
  package-download-transaction((#s(package-desc :name better-defaults :version (20160601 1219) :summary "Fixing weird quirks and poor defaults" :reqs nil :kind single :archive "melpa" :dir nil :extras ((:url . "https://github.com/technomancy/better-defaults") (:keywords "convenience")) :signed nil)))
  package-install(better-defaults)
  (if (package-installed-p package) nil (package-install package))
  (lambda (package) (if (package-installed-p package) nil (package-install package)))(better-defaults)
  mapc((lambda (package) (if (package-installed-p package) nil (package-install package))) (better-defaults material-theme))
  eval-buffer(#<buffer  *load*> nil "/Users/adamg/.emacs" nil t)  ; Reading at buffer position 1437
  load-with-code-conversion("/Users/adamg/.emacs" "/Users/adamg/.emacs" t t)
  load("~/.emacs" t t)
  #f(compiled-function () #<bytecode 0x400d3941>)()
  command-line()
  normal-top-level()

When I put the code above in a separate init.el file, it doesn't throw an error, but it also doesn't change the startup screen or theme. Could someone point me to what I'm doing wrong?


Solution

  • Your package repository content (local cache) is outdated and Emacs is trying to download old, no longer existing version of the better package.

    From what I can see, problem is with this part:

    (when (not package-archive-contents)
      (package-refresh-contents))
    

    Basically, you refresh package contents from repositories only when there is no information at all (so only once, ever). In my opinion it's good to have it up to date all the time, even if it takes few seconds more for Emacs to start.

    Recomendation - remove condition for package refresh, change this two lines to simple:

    (package-refresh-contents)
    

    Oh, and briefly on config files - Emacs tries to load configuration from ~/.emacs or ~/.emacs.d/init.el file - in that order and only from one of them. So if you have .emacs file, init.el will be ignored. That's why your changes to init.el didn't throw any errors - Emacs ignored that file and used only .emacs.

    (BTW. Emacs also remembers which file was used, so when you customize configuration using Emacs built-in tools, changes go to the right file)