Search code examples
pythonemacsemacs24elpy

Can't save file: "autopep8 command not found"


I'm running python in emacs. I've written some code, and now I go to save the code using C-x C-s as normal. However, instead of saving, autopep8 command not found appears in the minibuffer. I can't save my code.

What can I do to restore function to emacs and save my file?

Here is my init.el

;; init.el --- Emacs configuration

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

(require 'package)

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

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

(defvar myPackages
  '(better-defaults
    paredit
    idle-highlight-mode
    ido-ubiquitous
    find-file-in-project
    smex
    scpaste
    ein
    elpy
    flycheck
    material-theme
    py-autopep8))
(package-refresh-contents)

(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

;; PYTHON CONFIGURATION
;; --------------------------------------

(elpy-enable)
(elpy-use-ipython)

;; use flycheck not flymake with elpy
(when (require 'flycheck nil t)
  (setq elpy-modules (delq 'elpy-module-flymake elpy-modules))
  (add-hook 'elpy-mode-hook 'flycheck-mode))

;; enable autopep8 formatting on save
(require 'py-autopep8)
(add-hook 'elpy-mode-hook 'py-autopep8-enable-on-save)

;; init.el ends here

Solution

  • Install the package providing the command autopep8 or remove the lines

    ;; enable autopep8 formatting on save
    (require 'py-autopep8)
    (add-hook 'elpy-mode-hook 'py-autopep8-enable-on-save)
    

    from your configuration. Installing can be done using pip or your package manager, depending on your system. On Ubuntu, this would be

    sudo apt-get install python-autopep8
    

    or

    pip install autopep8