Friday, 29 November 2013

Ready for Python development with Emacs in just 60 seconds

This post demonstrates how you can configure a very decent environment for Python development with Emacs, in just 60 seconds.

A dream is now true: The first time you start Emacs, it automagically downloads and configures all plugins you need.  Emacs is then just ready to work and you can start typing code immediately. 

For the impatient


1. Save configuration files you eventually have!

  $ cd $HOME 
  $ tar cpf dot-emacs.ORIGINAL.tar.gz .emacs .emacs.d
  $ mv .emacs   dot-emacs.ORIGINAL
  $ mv .emacs.d dot-emacs.d.ORIGINAL

2. Remove any Emacs configuration files you eventually have.

  $ rm -r -f .emacs .emacs.d

3. Install Python libraries

This should be done preferably inside a virtual environment.

   $ workon py276 #-- py276 is a virtualenv I'm using
   $ pip install epc
   $ pip install jedi
   $ pip install elpy

4. Download my .emacs file onto your home folder.

  $ cd $HOME 
  $ wget https://raw.github.com/frgomes/dot-emacs/master/dot-emacs.el
  $ ln -s dot-emacs.el .emacs

4. Start emacs. It will configure itself when it first run!

  $ emacs test.py

Features in a nutshell

  • python-mode, cython-mode and nxml-mode: ditto
  • jedi: provides auto completion
  • flymake: highlight syntax errors as you type

Contribute


Please let me know if you find issues. In particular, I don't have Windoze boxes, so the automagic configuration thing was never tested on it.

This script was designed to run on Emacs 23 onwards but only tested on Emacs 24. Please let me know if you find issues.

Please point out typos and bad English.

You can also suggest plugins or tools I missed. This is very much appreciated and may benefit my workflow as well :)
So... thanks a lot for your suggestion!

Known Issues

If you are behind firewall, you may (or may not) face download problems which involves HTTPS protocol. As far as I know, this is a bug on a third party library which Emacs depends on.

If Emacs opens the message window and vomits hundreds of errors coming from file cython-mode.el ... that's because your proxy server refused the https request and returned an error message in HTML. It's easy to fix this issue:

  $ cd $HOME/.emacs.d/plugins
  $ rm cython-mode.el
  $ wget https://raw.github.com/cython/cython/master/Tools/cython-mode.el

Chances are that now cython-mode.el is OK, since wget performs the request the way it needs to be in order to work properly.


That's it: ready for coding in 60 seconds :)

Cheers

-- Richard Gomes

8 comments:

  1. Not actually seeing any link to a script or .emacs file, just 'https://bit.ly/....' which doesn't go anywhere

    ReplyDelete
    Replies
    1. My mistake. Please try again. the article looks correct now.

      Delete
  2. Hi -- I'd like to try this out -- I see a dot-emacs.el file at:

    http://bazaar.launchpad.net/~frgomes/+junk/dot-emacs/view/head:/dot-emacs

    but I'm assuming that the bit.ly url points to some other version?

    thanks, Phil Austin

    ReplyDelete
    Replies
    1. Yes, I have it in Launchpad too. I've recently published in Github. That's the most recent version.

      My mistake. Please try again. The article looks correct now.

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Loading 00debian-vars...done
    Loading /etc/emacs/site-start.d/50autoconf.el (source)...done
    Loading /etc/emacs/site-start.d/50dictionaries-common.el (source)...
    Loading debian-ispell...
    Loading /var/cache/dictionaries-common/emacsen-ispell-default.el (source)...done
    Loading debian-ispell...done
    Loading /var/cache/dictionaries-common/emacsen-ispell-dicts.el (source)...done
    Loading /etc/emacs/site-start.d/50dictionaries-common.el (source)...done
    Loading /etc/emacs/site-start.d/50pylint.el (source)...
    Loading pylint...done
    Loading /etc/emacs/site-start.d/50pylint.el (source)...done
    Loading /etc/emacs/site-start.d/50python-docutils.el (source)...done
    Loading flymake...done
    [google-this configured]
    [auto-complete configured]
    [jedi configured]
    Loading paren...done
    For information about GNU Emacs and the GNU system, type C-h C-a.
    (New file)
    Can't guess python-indent-offset, using defaults: 4
    Loading semantic/db-file...done
    deferred error : (error Server may raise an error. Use "M-x epc:pop-to-last-server-process-buffer RET" to see full traceback:
    Traceback (most recent call last):
    File "/home/monte/.emacs.d/elpa/jedi-20130714.1415/jediepcserver.py", line 315, in
    main()
    File "/home/monte/.emacs.d/elpa/jedi-20130714.1415/jediepcserver.py", line 311, in main
    jedi_epc_server(**vars(ns))
    File "/home/monte/.emacs.d/elpa/jedi-20130714.1415/jediepcserver.py", line 211, in jedi_epc_server
    )
    user-error: Minibuffer window is not active
    deferred error : (error Server may raise an error. Use "M-x epc:pop-to-last-server-process-buffer RET" to see full traceback:
    Traceback (most recent call last):
    File "/home/monte/.emacs.d/elpa/jedi-20130714.1415/jediepcserver.py", line 315, in
    main()
    File "/home/monte/.emacs.d/elpa/jedi-20130714.1415/jediepcserver.py", line 311, in main
    )
    user-error: Minibuffer window is not active
    deferred error : (error Server may raise an error. Use "M-x epc:pop-to-last-server-process-buffer RET" to see full traceback:
    Traceback (most recent call last):
    File "/home/monte/.emacs.d/elpa/jedi-20130714.1415/jediepcserver.py", line 315, in
    main()
    File "/home/monte/.emacs.d/elpa/jedi-20130714.1415/jediepcserver.py", line 311, in main
    jedi_epc_server(**vars(ns))
    File "/home/monte/.emacs.d/elpa/jedi-20130714.1415/jediepcserver.py", line 211, in jedi_epc_server
    import_jedi()
    File "/home/monte/.emacs.d/elpa/jedi-20130714.1415/jediepcserver.py", line 259, in import_jedi
    )

    ReplyDelete
    Replies
    1. Hello Monte,

      I've just run from scratch in my computer and it works fine, except the issue I've already documented regarding protocol https: when retrieving cython-mode.el.

      Some basic verifications:

      1. Which operating system you are on and which version/distribution?

      2. Which version of Emacs?

      3. Which version of Python?

      4. What happens when you do the following in the command line?

      $ python /home/monte/.emacs.d/elpa/jedi-20130714.1415/jediepcserver.py

      5. Are you installing straight onto the operating system or inside a virtualenv?

      6. Are you behind a proxy server? Please let me know the result of:

      $ env | fgrep -i proxy

      Thanks

      -- Richard

      Delete
    2. Hi Monte,

      Step 3 was missing in the original post.
      Could you please try again?

      3. Install Python libraries
      This should be done preferably inside a virtual environment.
      $ workon py276 #-- py276 is a virtualenv I'm using
      $ pip install epc
      $ pip install jedi
      $ pip install elpy

      Thanks

      Delete