Using multiple Python shells in Emacs 'python-mode' with Python or IPython
Asked Answered
M

3

6

Is there a way to force a new instance of python-shell while running Emacs? It would be convenient when working on multiple projects with separate working directories (and different sets of modules).

Any attempt to invoke python-shell will only pull up the current instance.

Maloriemalory answered 26/11, 2011 at 21:29 Comment(0)
I
4

You need to rename your original python-shell before opening up a new one. Use M-x rename-buffer.

Isidroisinglass answered 26/11, 2011 at 22:10 Comment(0)
L
3

Renaming the buffer doesn't work for me, but you can use the third parameter of run-python.

M - : (run-python nil nil t)RET

Since the binding to switch to the current buffer isn't really helpful you can rebound it to something more useful

(defun my-run-python (&optional new)
  (interactive "P")
  (if new
   (run-python nil nil new)
   (pop-to-buffer (process-buffer (python-proc)) t)))

(define-key python-mode-map (kbd "C-c C-z") 'my-run-python)

And use C-cC-z to switch to the current python interpreter and C-uC-cC-z to switch to a fresh python interpreter.

Latish answered 29/11, 2011 at 12:47 Comment(1)
Thanks. This is great except it's for with loveshack python and I'm using python-mode. I wanted to switch over at some point but now I'm using iPython, which seems only compatible with python-mode.Maloriemalory
D
0

When using python-mode via python.el, having one Python shell per Python buffer is the default.

However, you can change this default behavior if what you want instead is for multiple Python buffers to share the same Python shell. To do so, after opening the first Python buffer, enter:

M-x python-set-proc

...which is documented:

Set the default value of `python-buffer' to correspond to this buffer.
If the current buffer has a local value of `python-buffer', set the
default (global) value to that.  The associated Python process is the
one that gets input from C-c C-r et al when used in a buffer that
doesn't have a local value of `python-buffer'.

Then later, if you want a new Python buffer to use its own shell, enter:

M-x set-variable python-buffer [RET] nil [RET]

After doing so and then opening a new Python buffer, a new Python shell will be created for that buffer after entering python-switch-to-python or C-c C-z.

Dvandva answered 16/1, 2015 at 16:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.