For ipython, we need to tell it to use emacs as the editor.
You can add the line to your ~/.bashrc by running this command if your
.emacs file is already setup for doing C-c C-c for sh. But you can
also copy this to a shell and run it.
cat <<EOF >> ~/.bashrc
# The default editor will be to use emacsclient to talk to emacs
# emacs must already be running for this to work correctly
export EDITOR='emacsclient'
EOFOr you can just edit your ~/.bashrc and put this as the last line:
export EDITOR='emacsclient'; Must have org-mode loaded before we can configure org-babel
(require 'org-install)
; Some initial langauges we want org-babel to support
(org-babel-do-load-languages
'org-babel-load-languages
'(
(sh . t)
(python . t)
))I recommend that you add these languages too, as you might want them later.
(R . t)
(ruby . t)
(ditaa . t)
(dot . t)
(octave . t)
(sqlite . t)
(perl . t);;; Emacs server
; Do not close the file that was being edited when C-x # is typed
(setq server-kill-new-buffers nil)
; Start the emacs server for emacsclient
(server-start)Setup the org-mode short cuts for agenda:
; Add short cut keys for the org-agenda
(global-set-key "\C-cl" 'org-store-link)
(global-set-key "\C-cc" 'org-capture)
(global-set-key "\C-ca" 'org-agenda)Short cuts for running programs and going to the next problem (or grep)
; Setup custom shortcuts
(global-set-key "\C-x\C-g" 'goto-line)
(global-set-key [f1] 'compile)
(global-set-key [f2] 'next-error)If you do not restart emacs right after making the changes to the .emacs file, you will not see the changes that we have made.
- Stop emacs -> C-x C-c
- Start emacs again
There are many many ways that you can run python. I will walk through a good number of them so that you can see what is going on.
Type just python and press enter. Type some math like “1+2”, but
without the quotes. Type exit() and enter when done to get out.
researchtools@ubuntu:~$ python Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 1+2 3 >>> exit() researchtools@ubuntu:~$
researchtools@ubuntu:~$ python -c 'print 1+2' 3 researchtools@ubuntu:~$
You can also tell python to run the code in a file. This will create a file called hello.py in the bash shell:
cat << EOF > hello.py
print 'hello world'
EOFNow run it like this:
researchtools@ubuntu:~$ python hello.py hello world researchtools@ubuntu:~$
cat <<EOF > hello2.py
#!/usr/bin/env python
print 'hello world2'
EOF
# Make the file "executable"
chmod +x hello2.py
./hello2.pystart ipython (not “python”)
In [1]: 1+2 Out[1]: 3
In [2]: ls -l total 8 -rw-r--r-- 1 researchtools researchtools 44 2011-10-09 10:46 hello2.py -rw-r--r-- 1 researchtools researchtools 20 2011-10-09 10:46 hello.py In [3]: run hello hello world In [4]: run hello.py hello world In [5]: run hello2 hello world2
Note that the 7 entry does not print hello world. The “module” hello was already loaded. We have to ask for it to be “reloaded.”
In [6]: import hello hello world In [7]: import hello In [8]: reload hello ------> reload(hello) hello world Out[8]: <module 'hello' from 'hello.pyc'>
Start emacs. Create a file, video8.py, that looks like this:
print 'start of my script'
print 'more stuff'
print 3+8Press C-c C-c in the file (aka buffer). You should now see a new
buffer appear called:
*Python Output*
In that buffer you should see the output of the 3 commands that were run.
start of my script more stuff 11
Highlight a region of the code:
- Move to the beginning of the file: M-<
- Start marking a region: C-space
- Move down a line: C-n
- Run the region: C-c |
Your Python Output buffer should contain the results for just the part you ran:
start of my script
- Start the python shell inside of emacs
- Type 1+2 inside the buffer after the “>>>”
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 1+3 4 >>>
Go back to your code window, select a region and run it with C-c |.
You will see that run in that python shell:
>>> ## working on region in file /tmp/python-2213Oe2.py... start of my script >>>
Type M-x compile. You will be prompted with “make -k”. Delete that
text and put in:
python hello.py
Press enter and you should see python run:
-*- mode: compilation; default-directory: "/home/researchtools/video/8/" -*- Compilation started at Sun Oct 9 11:05:46 python hello.py hello world Compilation finished at Sun Oct 9 11:05:46
Try it again with the script that does not need the python in
front. Replace python hello.py with ./hello2.py:
-*- mode: compilation; default-directory: "/home/researchtools/video/8/" -*- Compilation started at Sun Oct 9 11:07:50 ./hello2.py hello world2 Compilation finished at Sun Oct 9 11:07:50
We have made an alias for compile. Try pressing the F1 function
key. This will also bring up the compile command. Try using the
arrow keys to scroll up and down through previous compile commands.
Add a line like this to the end of video8.py. This is not valid python code.
print nothing goodUse C-c C-c to run the file. You should see this in the python
output buffer and your cursor will be sitting on the line where the
computer thinks your proble is located:
File "<stdin>", line 6
print nothing good
^
SyntaxError: invalid syntax
Move your cursor to the beginning of the file with M-<
Try it again but with the compile command: F1 or M-x compile.
python video8.py
You will now see an error:
-*- mode: compilation; default-directory: "/home/researchtools/video/8/" -*-
Compilation started at Sun Oct 9 11:12:46
python video8.py
File "video8.py", line 6
print nothing good
^
SyntaxError: invalid syntax
Compilation exited abnormally with code 1 at Sun Oct 9 11:12:46
But your cursor has not moved. You need to run the command M-x
next-error or F2 (the alias we created) to have emacs move your
cursor to the line where it thinks the problem occured.
Move the cursor inside of this SRC block and press C-c C-c
print 'hello org-babel'Nothing happens.
return 'hello org-babel try 2'