1

Like suggested in this question How to automatically run inferior process when loading major mode for the first time? I want to automatically run run-python when entering python major mode. However,

(add-hook 'python-mode-hook 'run-python)

doesn't work and gives the error

Wrong number of arguments: (1 . 3), 0

Which argument is needed by run-python? How to automatically start an inferior python interpreter when entering python-mode?

Edit: After M-x toggle-debug-on-error the output gives

Debugger entered--Lisp error: (wrong-number-of-arguments (1 . 3) 0)
run-python()
run-hooks(change-major-mode-after-body-hook prog-mode-hook python-mode-hook)
apply(run-hooks (change-major-mode-after-body-hook prog-mode-hook python-mode-hook))
run-mode-hooks(python-mode-hook)
python-mode()
call-interactively(python-mode record nil)
command-execute(python-mode record)
execute-extended-command(nil "python-mode")
call-interactively(execute-extended-command nil nil)
command-execute(execute-extended-command)
3
  • 1
    What you tried would actually work. Technically run-python doesn't require any arguments, at least not in the version of Emacs that I have. You could do C-h f run-python to see what's the function signature you have, though I'm almost sure the error comes from some other place. M-x toggle-debug-on-error would help to find the problem.
    – wvxvw
    Sep 5, 2015 at 16:42
  • I'm using Emacs ver. 24.5.1 and also tried an empty init.el with only (add-hook 'python-mode-hook 'run-python) in it. C-h f run-python gives the signature (run-python CMD &optional DEDICATED SHOW). For debug output see edited question above. I tried an Emacs ver. 23.4.1 on a different machine and indeed there is (add-hook 'python-mode-hook 'run-python) working as expected.
    – max0r
    Sep 5, 2015 at 17:04
  • For some reason on your machine run-python requires CMD argument... well, if that's the case you could change your code to (add-hook 'python-mode-hook (lambda () (run-python (python-shell-calculate-command))).
    – wvxvw
    Sep 5, 2015 at 17:06

1 Answer 1

3

If you look at documentation of run-python, C-hfrun-python you would notice that requires on parameter cmd. When that command is called interactively emacs calculates that parameter (see Using interactive or C-hfinteractive to understand the mechanism). However when called from elisp, the cmd need to be explicitly passed.

There are couple of ways to solve this -

1) Write a custom function which calls run-python with proper arguments, something like following, and add it to python-mode-hook

(defun my-run-python ()
    (run-python (python-shell-parse-command)))

EDIT

After understanding your problems with the above approach I came up with the following function, hopefully this is what you are looking for

(defun my-run-python ()
  (save-selected-window
    (switch-to-buffer-other-window (process-buffer (python-shell-get-or-create-process (python-shell-parse-command))))))

2) Add the function python-shell-switch-to-shell which does not expect any arguments, to python-mode-hook

4
  • Is it possible to see which arguments are passed to run-python when called interactively? Neither your first suggestion nor the second gives the same result as calling run-python or pressing C-c C-p. The first one doesn't create a new window with the python process (I'll have to do it manually) and the second asks me to manually input the parameters every time.
    – max0r
    Sep 6, 2015 at 9:38
  • I accomplished it with my basic knowledge of Emacs Lisp: (defun run-python-once () (remove-hook 'python-mode-hook 'run-python-once) (switch-to-buffer (last-buffer)) (run-python (python-shell-parse-command)) (split-window-sensibly) (switch-to-buffer-other-window (python-shell-get-buffer)) ) added to python-mode-hook. But that doesn't seem pretty straightforward to me.
    – max0r
    Sep 6, 2015 at 11:52
  • The problem with (lambda () (run-python (python-shell-parse-command) nil t)) added to python-mode-hook is, that an opened file doesn't show up. A new window is created and the inferior python process is shown, but the first window dosen't change to the newly opened file buffer.
    – max0r
    Sep 6, 2015 at 12:49
  • @max0r I have edited the answer to mention an alternative approach hopefully it resolves your current issues. Sep 6, 2015 at 13:13

Your Answer

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

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