Tampilkan postingan dengan label Python. Tampilkan semua postingan
Tampilkan postingan dengan label Python. Tampilkan semua postingan

Agustus 21, 2012

Change Python Interpreter Temporary (python3 -> python2)

I came across this difficulty when I try to install ArangoDB. It comes with V8 Javascript engine from Google bundled in. Installing V8 requires Python 2, not Python 3 which is the default in my Arch Linux box.

[bpdp@bpdp-arch V8]$ ls `which python`
lrwxrwxrwx 1 root root 7 Apr 24 06:48 /usr/bin/python -> python3
[bpdp@bpdp-arch V8]$

Luckily, V8 uses "#/usr/bin/env python", not "#/usr/bin/python". To change this behaviour, all I have to do is just ask "env" to use my python2 interpreter. Here's how:

1. Create symlink to python2 under the name "python"
I put the symlink into my "$HOME/bin" directory.
$ cd ~/bin
$ ln -s /usr/bin/python2 python

2. Put into PATH

$ export PATH=~/bin:$PATH

"~/bin" should preced $PATH because env use the first setting.

[bpdp@bpdp-arch arangodb]$ which python
/home/bpdp/bin/python
[bpdp@bpdp-arch arangodb]$ env python
Python 2.7.3 (default, Apr 24 2012, 00:06:13) 
[GCC 4.7.0 20120414 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

There you go!

Desember 24, 2010

Mengaktifkan command line editing (Readline) di Python


Secara default, pada saat kita masuk ke python command line, tidak akan ada completion seperti halnya bila kita menggunakan bash, tcsh, atau zsh. Untuk mengaktifkan tab completion, biasanya kita menggunakan langkah-langkah berikut:
[bpdp@bpdp-arch ~]$ python
Python 3.1.3 (r313:86834, Dec  1 2010, 12:35:23)
[GCC 4.5.1 20101125 (prerelease)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import readline
>>> import rlcompleter
>>> readline.parse_and_bind(“tab: complete”)
Untuk mengotomatiskan langkah-langkah tersebut sehingga tidak perlu mengeksekusi berbagai perintah diatas, gunakan langkah-langkah sebagai berikut (asumsi: OS Linux dengan shell Bash):
Buat file $HOME/.pythonrc. Isinya:
import readline
import rlcompleter
readline.parse_and_bind(“tab: complete”)
Tambahkan env var yang diperlukan di $HOME/.bashrc
export PYTHONPATH=~/.pythonrc
Selesai
Saat masuk ke command line Python, otomatis mode command line editing telah tersedia dengan menekatan tombol tab, sama halnya dengan di Bash:
[bpdp@bpdp-arch ~]$ python
Python 3.1.3 (r313:86834, Dec  1 2010, 12:35:23)
[GCC 4.5.1 20101125 (prerelease)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> help(
Display all 163 possibilities? (y or n)