0

This answer how to map C-c ! to create a new python shell for the current buffer.

The main code is this:

(defun my-python-start-or-switch-repl (&optional msg)
  "Start and/or switch to the REPL."
  (interactive "p")
  (if (python-shell-get-process)
      (python-shell-switch-to-shell)
    (progn
      (run-python (python-shell-calculate-command) t t)
      (python-shell-switch-to-shell)
      )
    )
  )

As it can be seen, it uses run-python, so it only opens a shell that has the name of the buffer it was opened it from, e.g., if the program is foo.py, then the shell has the name: *Python[foo.py]*, but it doesn't run foo.py. Is there a way to have dedicated shells that by default run the program from which they being generated?

Schach21
  • 33
  • 8

1 Answers1

1

You should be able to do what you need modifying your function like this:

defun my-python-start-or-switch-repl (&optional msg)
"Start and/or switch to the REPL."
(interactive "p")
(if (python-shell-get-process)
    (progn
      (python-shell-send-buffer)
      (python-shell-switch-to-shell)
      )
  (progn
    (run-python (python-shell-calculate-command) t t)
    (other-window 1)
    (python-shell-send-buffer)
    (python-shell-switch-to-shell)
    )
  )
)

The main change is the call to python-shell-send-buffer that sends the entire current buffer to the running Python process.

Marioba
  • 736
  • 4
  • 10