0

I have the following code to auto format python code using autopep8:

(defcustom python-autopep8-path (executable-find "autopep8")
  "autopep8 executable path."
  :group 'python
  :type 'string)

(defun python-autopep8 ()
  "Automatically formats Python code to conform to the PEP 8 style guide.
$ autopep8 --in-place --aggressive --aggressive <filename>"
  (interactive)
  (when (eq major-mode 'python-mode)
    (shell-command 
     (format "%s --in-place --aggressive %s" python-autopep8-path
             (shell-quote-argument (buffer-file-name))))
    (revert-buffer t t t)))

(bind-key "C-c C-a" 'python-autopep8)

When I am setting up emacs and use for the first time it works perfectly, but then when I close emacs with M-x kill-emacs and re open emacs again and try to use it, I get the error: /bin/bash: line 1: nil: command not found.

I found this question on Stackoverflow where that person gets a similar error. They solved it apparently by setting the proper path to the variable PATH using M-x setenv. I was trying to do the same, but I am not sure what path to use and I tried different paths but kept getting the same error. I'd appreciate any help. I suspect the solution will be simple, but I just don't know it.

16
  • What is the path to the executable autopep8? Is it on the path in your shell startup files? Which OS are you using?
    – mmmmmm
    Nov 28, 2021 at 22:15
  • I am using fedora 35. Let me see how to find that path.
    – Schach21
    Nov 28, 2021 at 22:16
  • When I run echo $PATH I get /home/carlos/.virtualenvs/virtual_env/bin:/home/carlos/.local/bin:/home/carlos/bin:/usr/local/bin:/usr/bin:/usr/loca\ l/sbin:/usr/sbin
    – Schach21
    Nov 28, 2021 at 22:21
  • What is the value in emacs of python-autopep8-path after reopening?
    – mmmmmm
    Nov 28, 2021 at 22:22
  • 1
    You can change your code to this: (defcustom python-autopep8-path (or (executable-find "autopep8") "autopep8") so that if executable-find can't find it, you still get a valid string value.
    – phils
    Nov 29, 2021 at 0:57

1 Answer 1

1

Thanks to @phils who helped to come up with this piece of code that works:

(defcustom python-autopep8-path (or (executable-find "autopep8") "autopep8")
  "autopep8 executable path."
  :group 'python
  :type 'string)

(defun python-autopep8 ()
  "Automatically formats Python code to conform to the PEP 8 style guide.
$ autopep8 --in-place --aggressive --aggressive <filename>"
  (interactive)
  (when (eq major-mode 'python-mode)
    (shell-command 
     (format "%s --in-place --aggressive %s" python-autopep8-path
             (shell-quote-argument (buffer-file-name))))
    (revert-buffer t t t)))

(bind-key "C-c C-a" 'python-autopep8)

Your Answer

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

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