2

Typically when I send code from a Python script using C-c C-e to IPython, IPython does not echo the code that I input. For example, sending a script with just the following in test.py

2 + 2

and running it via C-c C-e will result in the following:

In [1]:
Out[1]: 4

Occassionally, it'll result in the following:

In [1]:
__PYTHON_EL_eval("2 + 2", "/Users/cdesjardins/foo.py")
Out[1]: 4

I would prefer that IPython always echos my input. Ideally, just saying:

In [1]: 2 + 2
Out[1]: 4

I am not sure how to troubleshoot this. I am using eglot and pylsp but when I disable eglot I am still getting this issue.

1
  • 1
    AFAICT, there is no provision for that: you get a message of the string sent in the echo area, but it is not inserted into the *Python* buffer. You'd need to change the code in python-shell-send-string so that the string is inserted in the buffer.
    – NickD
    Apr 6, 2023 at 1:48

2 Answers 2

4

This seems to be a somewhat 'intricate' problem. The problem is that Emacs uses python-shell-send-string to retrieve all kinds of information from the interactive shell in the background. One of the 'required' configurations to make this work correctly, is to start ipython with the --simple-prompt argument. But when starting ipython like this, input is no longer inserted (displayed) into the shell when using process-send-string. However, starting ipython without the argument, breaks the 'information retrieval functionality'. So, that is no option either.

Now, starting ipython without the --simple-prompt argument won't work, neither will changing (in some straightforward way) the python-shell-send-string function.

So then, I guess, the most straightforward solution is to create a custom command that simply copies the 'statement' to the ipython shell buffer, and then sends it from there. Starting from the python-shell-send-statement function, we could come up with a most basic 'custom' command that looks as follows:

(defun my-python-shell-send-statement ()
  (interactive)
  (let* ((region (if (region-active-p)
                     (cons (region-beginning) (region-end))
                   (cons
                    (save-excursion (python-nav-beginning-of-statement))
                    (save-excursion (python-nav-end-of-statement)))))
         (string (buffer-substring (car region) (cdr region))))
    (with-current-buffer (process-buffer (python-shell-get-process-or-error))
      (insert string)
      (comint-send-input))))

The above command can be used at least for sending single line statements. It can also be used for sending simple regions, but I guess it will break for more complex regions (although it might also just work for those).

Similarly, we could create 'custom' versions of the other python-shell-send-... functions, although it might take some extra work to get the strings formatted correctly in the ipython shell buffer (also the code above might work fine enough already; I have not tested it thoroughly).

This would be my first 'naive' approach, and it seems to work fine, at least for 'single line' statements. You can try the above command by evaluating it and using it via M-x or by binding it to your preferred key.

2
  • This sounds very sensible, but I also have this issue on Mac OS X and I did not have this issue in the past. I'm surprised to find so few questions and solutions for this issue if it was this general.
    – guibor
    May 1, 2023 at 17:46
  • 1
    Actually starting ipython without the --simple-prompt argument removed the annoying __PYTHON_EL_eval but inputs were still not echo'ed. I then reverted to using elpy which I had been using in the past and used (setq elpy-shell-echo-output t) (setq elpy-shell-echo-input t) (setq elpy-shell-add-to-shell-history t) and this works as I wanted it to.
    – guibor
    May 2, 2023 at 5:39
0

Not sure how fixed you are on using ipython, but I have found a way to display the input properly for python shells without using elpy (as I use LSP over elpy): https://stackoverflow.com/a/76495202/13029473

Hope this helps you find a solution that works for you as well.

2
  • In your link posted, you have a mistake - the second function should be dn/......
    – Ian
    Jun 17, 2023 at 11:58
  • Thanks for noticing, I fixed it now.
    – GonE
    Jun 19, 2023 at 12:33

Your Answer

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

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