Monday, December 26, 2011

Set up python in emacs

There are lots of version of python, We have to face different python in one system. I use python25 and python27 to develop GoogleAppEngine and use python26 to develop SinaAppEngine. I want to try python32. So following I will set up python with emacs step by step.

Firstly we install all python to be used. Such as python25, python26 and python27. I put all of them to "d:/home/program/".

Run emacs and find file .emacs. Add following code to set python27's root in emacs's path.

(setq pythonversion "27")
(setq program-path "d:/home/program/")
(setenv "PATH" (concat (concat program-path"python"pythonversion";") (getenv "PATH")))

Then add following function to switch between pythons.

(defun* zxy-switch-python-version (&optional (pver nil))
  "Swith python version at runtime"
  (interactive)
  (if (equal nil pver)
      (let ((tempver (read-string (format "[zxy] Swith to which version of python (default %s)? "
                                          pythonversion) nil nil pythonversion nil)))
        (setq pver tempver)))
  (unless (file-exists-p (concat program-path"python"pver""))
    (error (format "[zxy] Cannot find python%s!" pver)))
  (message (format "[zxy] Switch to python%s." pver))
  (setq pythonversion pver)
  (setenv "PATH" (concat (concat program-path"python"pythonversion";") (getenv "PATH"))))

Set up python-mode as following.

;; emacs python
(require 'python)
(defun zxy-python-compile ()
  "Use compile to run python programs"
  (interactive)
  (compile (concat "python " (buffer-name))))
(setq compilation-scroll-output t)
(define-key python-mode-map "\C-cc" 'zxy-python-compile)

Restart emacs and open a py file. M-x zxy-python-compile will run this file in a new buffer. M-x zxy-switch-python-version help to switch python version.

Using emacs and python to develop GoogleAppEngine project

Tools

  1. emacs.
  2. python25 or python27.
  3. GoogleAppEngine.
  4. appengine-emacs-toolkit. This toolkit can help to create project, start app server, browse in browser and upload.

Install

  1. Unzip emacs.
  2. Install python25 or python27 or both. Write python's folder to system path or emacs's path(refer to: Set up python in emacs).
  3. Install GoogleAppEngine.
  4. Clone appengine-emacs-toolkit repository or download and unzip appengine-emacs-toolkitV1.0.zip file.

Usage

  1. Run emacs.
  2. Copy appengine-emacs-toolkit to your emacs's load-path. Add following code to .emacs file.
    (setq gae-root-path (concat program-path"google_appengine/"))
    (setq gae-priority-python-version "27")
    (add-to-list 'load-path (concat plugin-p
    ath "appengine-emacs-toolkit"))
    (require 'appyaml-mode)
    (add-hook 'appyaml-mode-hook
              (lambda ()
                (progn (local-set-key "\C-cc" 'gae-start-appserver)
                       (local-set-key "\C-cb" 'gae-browse-appserver)
                       (local-set-key "\C-cu" 'gae-update-appserver))))
    

    The gae-root-path is your GoogleAppEngine's Installation path. plugin-path is where you put appengine-emacs-toolkit(e.g. d:/home/emacs-23/site-lisp/).

  3. Restart emacs.
  4. Call gae-new-project command in minibuffer. Input new project's path, name and python's version.
  5. New app.yaml file is open. Call gae-start-appserver command.
  6. Then a shell buffer came up. Run gae-start-appserver command or browse http://localhost:8001 manually.