unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to use a new buffer as Python shell?
@ 2022-08-18 15:40 edgar
  2022-08-18 15:55 ` Emanuel Berg
  2022-08-20  8:38 ` James Thomas
  0 siblings, 2 replies; 6+ messages in thread
From: edgar @ 2022-08-18 15:40 UTC (permalink / raw)
  To: help-gnu-emacs

Hi!

I know that "shell" can mean different things to new people. What I want 
is to have 2 (or n) Python buffers optionally linked to their respective 
2 (or n) Python interpreters (1 for each).

Right now, if I create a new buffer with python-mode and do C-c C-p 
(which executes run-python) or C-c C-z (python-shell-switch-to-shell), 
all buffers are linked to one interpreter (in *Python* buffer).

What I would like is to have something like C-u C-c C-z or M-x 
run-new-python (bogus function name) and get a prompt with the name of 
the new buffer, where another interpreter is running, and which I can 
use to send lines of code from the buffer with python-mode (the one from 
which the command run-new-python was issued).

I have seen that Org (org-babel) is able to send to new interpreters, if 
the :session value is changed. I think that for what I have been looking 
is similar in a way (hopefully makes it clearer)

Is this possible? I've been running around in circles online. Can you 
please help me? Thanks!

"GNU Emacs 28.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.33, 
cairo version 1.17.6) of 2022-04-28"

-------------------------------------------------
This free account was provided by VFEmail.net - report spam to abuse@vfemail.net
 
ONLY AT VFEmail! - Use our Metadata Mitigator to keep your email out of the NSA's hands!
$24.95 ONETIME Lifetime accounts with Privacy Features!  
15GB disk! No bandwidth quotas!
Commercial and Bulk Mail Options!  



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to use a new buffer as Python shell?
  2022-08-18 15:40 edgar
@ 2022-08-18 15:55 ` Emanuel Berg
  2022-08-20  8:38 ` James Thomas
  1 sibling, 0 replies; 6+ messages in thread
From: Emanuel Berg @ 2022-08-18 15:55 UTC (permalink / raw)
  To: help-gnu-emacs

edgar wrote:

> I know that "shell" can mean different things to new people.

Heroes in the half shell!

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to use a new buffer as Python shell?
@ 2022-08-19  9:48 Anders Munch
  0 siblings, 0 replies; 6+ messages in thread
From: Anders Munch @ 2022-08-19  9:48 UTC (permalink / raw)
  To: help-gnu-emacs@gnu.org; +Cc: edgar@openmail.cc

edgar@openmail.cc wrote:

> What I would like is to have something like C-u C-c C-z or M-x run-new-python (bogus function name) and get a prompt with the name of the new buffer, where another interpreter is running, and which I can use to send lines of code from the buffer with python-mode (the one from which the command run-new-python was issued).

You are using python.el.  You may have more luck with python-mode.el.

C-u M-x py-shell RET
starts a new dedicated interpreter subshell.  I'm not sure exactly what that means, but it looks like it does much of what you want.  You use C-u once, and on subsequent subshell actions python-mode knows to use the same one from the same .py buffer. 

One caveat, it looks like you can't rename the buffer to something with the name of the originating buffer, because then you break the connection between the .py buffer and the subshell.
Some lisp hacking may be required to teach python-mode to prompt for a better subshell buffer name. 

regards, Anders




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to use a new buffer as Python shell?
@ 2022-08-19 20:12 edgar
  0 siblings, 0 replies; 6+ messages in thread
From: edgar @ 2022-08-19 20:12 UTC (permalink / raw)
  To: Anders Munch; +Cc: help-gnu-emacs

On 2022-08-19 09:48, Anders Munch wrote:
> You are using python.el.  You may have more luck with python-mode.el.
>
> C-u M-x py-shell RET
> starts a new dedicated interpreter subshell.
> --- snip 8< ---
>
> regards, Anders

Thanks, Anders. When I
0. cd ~/.emacs.d/ && git clone  
https://gitlab.com/python-mode-devs/python-mode.git
1. emacs -q
2. evaluate

       (push "~/.emacs.d/eglot" load-path)
       (autoload 'eglot "eglot")
       (autoload 'ensure-eglot "eglot")
       (autoload 'eglot-ensure "eglot")
       (with-eval-after-load "eglot"
         (setq eldoc-echo-area-use-multiline-p 2))

       (push "~/.emacs.d/python-mode" load-path)
       (autoload 'python-mode "python-mode" "Python Mode." t)

       ;; Add Eglot to Python
       (add-hook 'python-mode-hook 'eglot-ensure)

       ;; Configuration for python-mode.el
       (add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode))
       (add-to-list 'interpreter-mode-alist '("python3" . python-mode))

3. Execute =C-x C-f myfile.py=  =(find-file "myfile.py")=
4. Issue =C-u M-x py-shell=

The frame is split into 2 windows. The buffer in which I was working  
is not shown (I have to =C-x b= to return). When I am back at the  
buffer with =myfile.py=, I run =M-x py-execute-line=. This creates a  
temporary buffer that I have to kill manually. Also, in step 3 I get

     Directory-local variables error: (error Command attempted to use  
minibuffer while in minibuffer)

Does anybody know if it is possible to avoid switching to a different  
buffer when =C-u M-x py-shell= is called? To avoid the temporary  
buffer, I added =(setq py-execute-no-temp-p t)=, at the end of the  
configuration. Now, the temporary buffer is not shown, but if I go to  
the REPL and type =np RET= twice, I have this:

     >>> >>> >>> np
     >>> np
     Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
     NameError: name 'np' is not defined
     >>>

(the first line of myfile.py is a comment. The second line--the one  
that I try to run--is =import numpy as np=). I also have a second  
*Python-xxxx* buffer which was automatically created. All this happens  
with or without the Eglot lines.

"GNU Emacs 28.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.33,  
cairo version 1.17.6)
  of 2022-04-28"
python-mode.el commit: 765af45, Tag: 6.3.0 (72)


-------------------------------------------------
This free account was provided by VFEmail.net - report spam to abuse@vfemail.net
 
ONLY AT VFEmail! - Use our Metadata Mitigator to keep your email out of the NSA's hands!
$24.95 ONETIME Lifetime accounts with Privacy Features!  
15GB disk! No bandwidth quotas!
Commercial and Bulk Mail Options!  



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to use a new buffer as Python shell?
  2022-08-18 15:40 edgar
  2022-08-18 15:55 ` Emanuel Berg
@ 2022-08-20  8:38 ` James Thomas
  2022-08-20 13:50   ` edgar
  1 sibling, 1 reply; 6+ messages in thread
From: James Thomas @ 2022-08-20  8:38 UTC (permalink / raw)
  To: edgar; +Cc: help-gnu-emacs

edgar@openmail.cc wrote:

> Right now, if I create a new buffer with python-mode and do C-c C-p
> (which executes run-python) or C-c C-z (python-shell-switch-to-shell),
> all buffers are linked to one interpreter (in *Python* buffer).

If you pass the prefix argument to run-python and choose the 'dedicated'
option, a new one is created for each buffer.

--



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to use a new buffer as Python shell?
  2022-08-20  8:38 ` James Thomas
@ 2022-08-20 13:50   ` edgar
  0 siblings, 0 replies; 6+ messages in thread
From: edgar @ 2022-08-20 13:50 UTC (permalink / raw)
  To: James Thomas; +Cc: help-gnu-emacs

On 2022-08-20 08:38, James Thomas wrote:
> edgar@openmail.cc wrote:
> 
>> Right now, if I create a new buffer with python-mode and do C-c C-p
>> (which executes run-python) or C-c C-z (python-shell-switch-to-shell),
>> all buffers are linked to one interpreter (in *Python* buffer).
> 
> If you pass the prefix argument to run-python and choose the 
> 'dedicated'
> option, a new one is created for each buffer.
> 
> --

This is it! (Can't believe I only tried C-u C-c C-z and not C-u M-x 
run-python. Thanks!

-------------------------------------------------
This free account was provided by VFEmail.net - report spam to abuse@vfemail.net
 
ONLY AT VFEmail! - Use our Metadata Mitigator to keep your email out of the NSA's hands!
$24.95 ONETIME Lifetime accounts with Privacy Features!  
15GB disk! No bandwidth quotas!
Commercial and Bulk Mail Options!  



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-08-20 13:50 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-08-19  9:48 How to use a new buffer as Python shell? Anders Munch
  -- strict thread matches above, loose matches on Subject: below --
2022-08-19 20:12 edgar
2022-08-18 15:40 edgar
2022-08-18 15:55 ` Emanuel Berg
2022-08-20  8:38 ` James Thomas
2022-08-20 13:50   ` edgar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).