1

I have been coding for a while using R and RStudio but recently I started to dig into python. It is fair to say that I am new to python, but moreover, new to editing the emacs .init file (.emacs) in detail. I am trying to set and understand the interplay between python and emacs, mainly IPython, since I suspect that I am going to be heavily using IPython in the near future.

At the beginning, my intention was only to be able to use .emacs to edit some code and then, if necessary, copy and paste into IPython. At that stage, the part of my .emacs file dedicated to python was:

;; Enable python
(add-to-list 'load-path "/sw/lib/python-mode-1.0")
(setq auto-mode-alist
      (cons '("\\.py$" . python-mode) auto-mode-alist))
(setq interpreter-mode-alist
      (cons '("python" . python-mode)
        interpreter-mode-alist))
(autoload 'python-mode "python-mode" "Python editing mode." t)    

I loaded /sw/lib/python-mode-1.0, which comes from installing fink a while ago, because is the only directory in which I found a python-mode.el file. At that time seemed the only sequence of commands that worked (I have the intuition that half of it is not needed(?)).

The I learned that you could send commands from .emacs to Python/IPython. I typed: M-x python-mode , and then M-x py-shell to work with "normal-python" and it worked. Here I would have to ask the 1st question.

1- Every time I run a command I obtain the message:

`## working on region in file /var/folders/bd/f2194wl90rg38c8jqc12m47m0000gn/T/python-13120iJs.py..`

How do I avoid obtaining this message (every time!). This question is less important than the second one below.

Then, based on this post: How to open IPython interpreter in emacs? I typed this into my .emacs file:

(when (executable-find "ipython")
  (setq
   python-shell-interpreter "ipython"
   python-shell-interpreter-args ""
   python-shell-prompt-regexp "In \\[[0-9]+\\]: "
   python-shell-prompt-output-regexp "Out\\[[0-9]+\\]: "
   python-shell-completion-setup-code
   "from IPython.core.completerlib import module_completion"
   python-shell-completion-module-string-code
   "';'.join(module_completion('''%s'''))\n"
   python-shell-completion-string-code
   "';'.join(get_ipython().Completer.all_completions('''%s'''))\n"))

This, effectively, allows me to use IPython by typing: M-x run-python. However, when I try C-c C-c I obtain the error message : C-c C-c is undefined. (same for C-c | , also undefined)

At this stage, I have these two "chunks" of python-set-up lines in my .emacs file and nothing seems to work properly. I have looked in the web and found this: http://ipython.org/ipython-doc/1/config/editors.html , so I also tried to type: (require 'ipython) and it did not work either. It says that file ipython is not found. Therefore, the 2nd and most important question in this post is:

2- Can someone help me to do a clean set up of IPython in Emacs? (I do not mind if I have to remove all the set up lines so far. I simply want to be able to work efficiently using Emacs+IPython)

I am using a Mac 10.9.5. Emacs version is 24.4. Python 2.7.8. And in case is of any use, I installed python using anaconda:

>which python
/anaconda/bin/python

>which ipython
/anaconda/bin/ipython

and emacs with macports, and is "aliased":

 >which emacs 
 emacs:   aliased to open -a /Applications/MacPorts/Emacs.app/Contents/MacOS/Emacs
serv-inc
  • 35,772
  • 9
  • 166
  • 188
Javier
  • 1,530
  • 4
  • 21
  • 48

3 Answers3

3

There are different python-mode's in parallel - nothing wrong so far, however the last loaded takes the keymap and also the menu. The keys missing are from python-mode.el - consult the menu "Python" in this case, which should indicate the other ones.

BTW py- TAB displays available commands from python-mode.el, while

python- TAB likewise from shipped python.el

WRT the first question, messaging: in most cases code isn't sent to interpreter-shell, but stored in a temporary file, whose result you'll see. The reason are coding issues - otherwise several bugs show up. So this message delivers good news - and is useful for debugging/tweaking.

Maybe consider a bug-report at https://bugs.launchpad.net/python-mode

Andreas Röhler
  • 4,804
  • 14
  • 18
  • Hi Andreas, thanks. Just a couple of things. Regarding to my question 1, do you mean that the message is a good thing, but still IS a bug to be reported at https://bugs.launchpad.net/python-mode? And secondly, when I type `M-x py-TAB` (if this is what you mean) the only completion possible is `python-mode`. In any case, for now, everything seems to work all right :) . Thanks again. – Javier Jan 04 '15 at 09:01
  • @Javier If M-x py-TAB doesn't display the commands, python-mode.el isn't loaded. BTW bug-reporting was indicated for completeness only - cheers. – Andreas Röhler Jan 05 '15 at 10:36
  • Hi Andreas.Yeah sorry, you are right. Yesterday when I wrote I did not realized that Python-mode was not on. I think it works for now (we'll see). Thanks again for your help. – Javier Jan 05 '15 at 10:50
  • Nonetheless, as I see now, when I type `M-x py-TAB` on an emacs window goes directly into `python-mode`. Now if I type again `M-x py-TAB` goes into `python-...` with lots of different completions. I am guessing I am not loading python-mode.el, correct? – Javier Jan 05 '15 at 11:01
  • Andreas, problem solved, effectively python mode was not loaded. THANKS! – Javier Jan 05 '15 at 11:06
  • By the way, just realized that I am using your maintained python-mode.el file, fantastic! :) – Javier Jan 05 '15 at 11:32
0

I suggest you start with using Emacs with no special configuration: Emacs-24.4 should just work out of the box for Python/IPython.

Stefan
  • 27,908
  • 4
  • 53
  • 82
0

I wrote a plug-in for Emacs that allows interfacing with ipython while in Markdown or TeX buffer. While editing Python code you press a key, the region with Python code is sent over to ipython and its results are displayed right underneath as verbatim output. If there is a plt.show() call, it is converted into a plt.savefig(..) and the image reference is included in Markdown or TeX.

emacs-ipython

BBSysDyn
  • 4,389
  • 8
  • 48
  • 63