0
OS: Windows 11 (64 bit)
Python version: 3.12.0 (64 bit)
Emacs version: 29.1

I use LSP mode in Emacs for Python programming and I have in my Emacs init.el file the following:

(custom-set-variables
        . . .
    '(python-shell-interpreter "python"))

The above code allows Emacs to detect the correct Python interpreter, that is, the current running virtual environment. As a matter of fact, I always activate in a command line my desired Python virtual environment, then I run Emacs from the very terminal as a background process. This way, Emacs detects automatically the current running Python virtual environment.

This works, except that Python inferior buffer doesn't seem to work properly or maybe it's just me who doesn't understand how this buffer functions. Consider the following Python program:

def main():
    """Print Hello."""
    print("Hello World", flush=True)


if __name__ == "__main__":
    main()

First I run C-c C-p to start a Python process (therefore a inferior Python buffer in Emacs) and then I run C-c C-c to send the entire buffer containing the above-mentioned program to Python. The problem is, no output, no "Hello world" is printed, only a new prompt line >>> is appended to the inferior Python. However, if there is an error in the program, let's say for example a syntax error, then the corresponding error message is printed after C-c C-c in inferior Python buffer.

I don't understand why error messages are printed but not the program's output? Do I need to add some configuration in my Emacs init.el file ?

0

1 Answer 1

1

First, the code under if __name__ == "__main__": function is only executed when running python as a program (see here for example).

Second, as far as I remember, when using python-shell-send-buffer and python-shell-send-file, no output is shown in the comint buffer. Try python-shell-send-statement or python-shell-send-region. For more info see here

EDIT in response to the comments

If you want to quickly run the file, then I would suggest adding the following to your Emacs config file

(add-hook 'python-mode-hook (lambda ()
             (setq-local compile-command (format "python %s" buffer-file-name))))

and run your program quickly via M-x (re)compile (use compile only the first time). You probably also want to add a keybinding for, at least, the recompile command.

You can run the (re)compile command in a comint buffer by prefixing the command with the universal argument (C-u).

END EDIT

2
  • Thank you. I know about if name, but until now, I've always used elpy for Python in Emacs. Using elpy, C-c C-c, runs the main function. Maybe it's the developer of the elpy package who does some technical stuff behind the scene to make it work. But recently I switched to LSP mode and I encountered this problem. Based on your suggestion I can confirm that selecting the main() line in the program followed by C-c C-e works. Another workaround that I have just tested is send the whole file C-c C-l. I would have preferred to be able to run everything just via C-c C-c without selecting anything
    – user17911
    Commented Jan 7 at 15:32
  • 1
    I've updated the answer with an 'alternative' solution. Commented Jan 7 at 16:38

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.