1

In Emacs, when I need to start a python interpreter, I have to run (python-shell-switch-to-shell) , but then I always have to confirm two things:

  1. Confirm the python-shell-interpreter-args.
  2. Confirm to make a dedicated process (y or n), it is really tedious to confirm them every time i need to start a python shell, is there a way to provide the default options and skip these steps?

Also, I found every time I have the python shell restarted, I have to manually run python-shell-send-setup-code again to make the shell works properly. it seems this setup function only will be called when you start a new python shell, 'cus it is a hooked function for the inferior python mode, if you don't leave the python shell buffer, and restart the python shell within the same buffer, this setup function won't be called. Is there a way to solve this? e.g., by override the original python-shell-send-setup-code or python-shell-switch-to-shell

1

I would recommend to use elpy as it comes with lot of such utilities.

You can use elpy-shell-switch-to-shell function which starts a python process and switches to shell.

If you dont want to install package for that, you can add this to your config.

(defun my/python-shell-switch-to-shell ()
  "Switch to inferior Python process buffer."
  (interactive)
  (pop-to-buffer (process-buffer (elpy-shell-get-or-create-process))))

(defun my/python-shell-get-or-create-process ()
  "Get or create an inferior Python process for current buffer and return it."
  (let* ((bufname (format "*%s*" (python-shell-get-process-name nil)))
         (proc (get-buffer-process bufname)))
    (if proc
        proc
      (run-python (python-shell-parse-command))
      (get-buffer-process bufname))))

and then you can run M-x my/python-shell-switch-to-shell

1

add this to your .emacs to use alt-p to run python or switch to an existing python buffer (you may need to change the "/usr/bin/python" string to the location of your python program):

(global-set-key [?\M-p]
  (lambda()
    (interactive)
    (pop-to-buffer (process-buffer
                    (python-shell-get-or-create-process
                     "/usr/bin/python" nil))
                   t)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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