Skip to content

Latest commit

 

History

History
209 lines (169 loc) · 10.5 KB

ome-python.org

File metadata and controls

209 lines (169 loc) · 10.5 KB

Oh My Emacs Python

This is part of oh-my-emacs.

Prerequisites

PackageWindowsUbuntu/Debian/MintArchLinuxFedoraMac OS XMandatory?
PythonpythonYes
pippython-pipYes
IPythonipythonNo
nose[pip][pip][pip][pip]No
virtualenv[pip][pip][pip][pip]No
pyenv[web][web][web][web]Yes

Notes:

  • Python, currently, I only tested Python 2.

El-get packages

PackageStatusDescription
elpyRequiredElpy is the one for all Emacs python solution.
pyenv.elRequiredEmacs integration for pyenv.

Elpy

The first time I tried to write Python with emacs, I’ve found there’re really really too many choices, which may make newbies confusing and disgusting.

Say python-mode, before Emacs 24.3, there’re at least three choices:

  • The old python.el from Emacs, which has some minor problems when dealing with docstring, Python 3k, etc.
  • The python-mode.el from the Python community, which provides lots of dirty menu items. It’s a tedious long journey to make all this menu items just work.
  • The wonderful python.el from fgallina, which overcomes many drawbacks of the old Emacs builtin python.el, and has been integrated to Emacs trunk. I think this is the best choice of the three.

Say code checking tools,

  • What is pep8?
  • What is pylint?
  • What is pyflakes?
  • What is flake8?
  • What’s the differences between all these fucking wonderful code checking tools? How and when should I use it? Which one to use? Any shortcuts to fire these tools within python-mode?

Say developing tools,

  • What is virtualenv?
  • What about code completion? Maybe after two days’ google, you finally make auto-complete works with a completion backend, such as rope or jedi, but how can you make completion be virtualenv-awared?

Choices, choices, choices, choices are where all happiness and headaches begins. Just google a minute you’ll find articles which use different python-mode, different dotemacs, different way to get necessary packages, are, what’s the fuck[1]! I’m just a newbie, any out-of-box solutions? Fortunately, there comes elpy, which saves our lifes.

Just like other big emacs packages, elpy has some external dependencies. But don’t be afraid, believe me, within minutes, you’ll get an out-of-box Emacs Python “IDE”. First, make sure you have python and pip installed, and then type pip install elpy with root priviledge[2], then choose a completion backend, either pip install rope or pip install jedi [3].

Make sure you have others necessary python tools installed:

  • pip install virtualenv
  • pip install pyflakes
  • pip install nose

If you’re lucky enough, you can fire up Emacs and start python work right now.

There’re other annoying problems, one of them is the division between Python 2 and Python 3. So you may need to replace pip with pip2, or python with python2, etc. Ah, different systems has different favors and naming rules.

The second thing is, elpy requires Emacs version >= 24.3, since Emacs 24.3 ships with a new python.el [4], which, actually, is the aforementioned fgallina’s python.el. So you may need to upgrade your builin python.el, but don’t worry, oh-my-emacs will handle this for you!

Third, current elpy use flymake as its syntax checker, while oh-my-emacs favors flycheck. Seems elpy has a plan to support both flycheck and flymake.

There’s also a emacs-for-python project in github, which attracts more github stars than elpy, but I don’t like its approach, since it will clean out all your existing dotemacs and replace with a wholly new, which is just useful for Python. However, elpy is just a plugin, though it implements lots of features with the help from other packages, you can still disable or enable elpy, without breaking your existing precious dotemacs.

Thanks elpy again, for time it saves for us, through which we can focus more on Python, on our life, instead of the details to make choices. Elpy is the ultimate out-of-box Emacs Python “IDE” solution.

By default, (elpy-enable) will modify auto-complete’s keymap, which would make auto-complete unoperative when you try to complete function arguments with auto-complete-clang by utilize yas-expand from YASnippet. I’ve spent a whole day to trace this problem and finally find here. So I use (elpy-enable t) instead. Please C-h f elpy-enable RET to get details.

From version 1.3, elpy replaced virtualenv.el with pyvenv, see here for details.

Also, elpy integrates with nosetests, C-c C-t will run nose on your project. So you should pip install nose if you want to use nosetests with elpy.

(when (version< emacs-version "24.3")
  (ome-install 'python24))

(defun ome-elpy-setup ()
  (elpy-enable)
  (setq elpy-rpc-backend "jedi")
  (when (executable-find "ipython")
    (elpy-use-ipython))
  (setq elpy-modules '(elpy-module-sane-defaults
                       elpy-module-company
                       elpy-module-eldoc
                       elpy-module-highlight-indentation
                       elpy-module-pyvenv
                       elpy-module-yasnippet))
  (define-key python-mode-map (kbd "RET")
    'newline-and-indent)
  (add-hook 'python-mode-hook
            (lambda ()
              (set (make-local-variable 'comment-inline-offset) 2)
              (auto-complete-mode -1))))

(ome-install 'elpy)

Some python web frameworks often provide a *.wsgi file to application initialization, which is just a plain python file.

(add-to-list 'auto-mode-alist '("\\.wsgi\\'" . python-mode))

pyenv

Just like Ruby, sometimes we need to work with different versions of Python, so it’s helpful if there’s some tool to help use manage and switch different versions of Python.

As I know, there’re three projects for this purpose:

However, pythonbrew is “no longer under active development” any more, pythonz is a fork of original pythonbrew, and pythonbrew recommand pyenv.

Actually, pyenv was forked from rbenv and ruby-build, so if you’re an old user of rbenv, you’re all right. It’s quite easy to get and install pyenv.

Emacs has a pyenv.el package for integrating pyenv with Emacs, which, was also forked from the corresponding rbenv.el.

(defun ome-pyenv-setup ()
  ;; when user installed pyenv via homebrew on Mac OS X
  (when (and (memq window-system '(mac ns))
             (file-exists-p "/usr/local/opt/pyenv"))
    (setq pyenv-installation-dir "/usr/local/opt/pyenv"))
  (require 'pyenv)
  (global-pyenv-mode t))

(when (file-exists-p "~/.pyenv/version")
  (ome-install 'pyenv))

Todo

[1] See here, here, here, here, and here. [2] pip install pkg with root can make pkg accessible in newly created virtualenvs. [3] See elpy wiki to get basic knowledge of elpy completion backend. [4] Check Emacs 24.3 release and python.el for the author’s blog.