write Python in Emacs and Org mode

I write this post during reading the book Learn Python3 the Hard Way. This is the first python book I read from page to page.

1 edit the source code in python mode

In Emacs Org mode, you can insert the source code using <s then <tab> . If you want to edit the source code, you can hit C-c ' then you will be switched to a buffer in python mode and all the features of python mode can be utilitized. After finishing the modification, , c will save the changes and switch you back to the Org file and , k will give up the changes and switch you back to the Org file.

2 set the command for evaluating the python code

(setq org-babel-python-command "python3")

3 delete the *.elc files

After org mode version 9, it is buggy to evaluate python code in Org mode and the error message is :

Evaluation of this python code is disabled

After some google, I find the solution at here . The solution is easy: deleting the .elc files under the .emacs.d/elpa/org-plus-contrib-XXXXXX/ directory.

4 export with the result

Take the following code snippet for example.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
cars = 100
space_in_a_car = 4
drivers = 30
passengers = 90
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven

print("There are",cars,"cars available.")
print("There are only",drivers,"drivers avaiable.")
print("There will be", cars_not_driven,"empty cars today.")
print("We can transport",carpool_capacity,"people today.")
print("We have",passengers,"to carpool today.")
print("We need to put ablout",average_passengers_per_car,"in each car")

After the above debugging and config, you can evalute the code snippet using C-c C-c inside the code. and after that the result will appear below the code as:

#+RESULTS:
: There are 100 cars available.
: There are only 30 drivers avaiable.
: There will be 70 empty cars today.
: We can transport 120 people today.
: We have 90 to carpool today.
: We need to put ablout 3.0 in each car

However, when I call the org export dispatcher using C-c C-e h o . The result part is not displayed as the code snippet does. After checking the Org manual, there is a variable org-export-use-babel controing whether evaluate the code snippet or not during exporting. Evan the variable is set to be t, the code is not evaluated. To control export the code, results or both, the :export header arguments should be set to code, results or both .

In summary, in order to control the evaluation and export, results and export header arguments are necessary.

5 change the systematic

If you install several versions of Python in your system, you have to choose one as the default. Execute the following to set the priority list:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 3

If you want to switch to a different version of Python, just execute the following comand :

sudo update-alternatives --config python

Then choose the right number from the popup menu.

6 install the distutils packages

After updating the python from 3.5 to 3.6, anaconda-mode does not work. In the minibuffer, it says:

InternalError: The subprocess /usr/bin/python3.6 has crashed.

It takes me much time to begin to read the anaconda-mode buffer the find the error message. what a stupid guy. The error message is

<module>
    from distutils.spawn import find_executable
ModuleNotFoundError: No module named 'distutils.spawn'

After several google, I install the python3-distutils using:

sudo apt-get install python3-distutils

Notice that it is python3-distutils , not python-distutils .

After this, the auto-completion is live again.

7 debug python in Emacs

During working on ex23.py, I have an idea to debug python in Emacs.

I know it is pdb that call the python debugger. However, when I use python3.6. I have to modify it to:

python3.6 -m pdb ex23.py utf-8 strict

Then, I found that there is an extension realgud , which even allows you use the F10 to next, F11 to step in, shift F11 to step out, F5 to run/continue. More important all the shortcuts are effective in the source code buffer, pretty handy.

You even can you mouse to set and clear breakpoints.

Related

comments powered by Disqus