Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs - Running current file in Python

Tags:

python

emacs

I am using ELPY in emacs in Windows. How do I eval current file inside emacs using a Shortcut.

When I use eval-buffer. I get below messages.

Flymake unable to run without a buffer file name

Removed if main == 'main' construct, use a prefix argument to evaluate.

like image 300
skvsree Avatar asked Jan 20 '15 02:01

skvsree


1 Answers

There is a built in feature in emacs when working with the python (inferior) shell, to prevent people from running the whole script unintentionally, which is does by redefining (or removing) __name__ before it runs.

Your script does send __name__ = "__main__" but it is overwritten, due to this 'safety feature'.

To execute as you wish, running it as main, use the key binding:

`C-u C-c C-c`

If you would like to remap this to be something quicker or more familiar, try something like this:

(global-set-key (kbd "<f7>") (kbd "C-u C-c C-c"))

this was discussed in this thread here, where there is also some related extra information.

like image 166
n1k31t4 Avatar answered Sep 28 '22 10:09

n1k31t4