Stole Python package ideas from Gabriel Elanaro’s git project. The question is whether I use Rope or Jedi for auto-completion. Rope, while claiming more features, seems to crash and lock up my Emacs connections, so I’m back to using Jedi…for now. See this article.
(packages-install '( elpy
nose
jedi
py-autopep8 ;; pylint
virtualenvwrapper))All development for Python is done in virtual environments. However, this is a monstrous collection of layers, so I am now using pyenv for all my Pythonic virtualization needs, as it does a better job of both virtualenvwrapper and autoenv:
brew install pyenv pyenv-virtualenv pyenv-virtualwrapperOr, if on Linux, let’s do it neckbeard-style:
git clone https://github.com/yyuu/pyenv.git ~/.pyenv
git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenvNext, use pip to install virtualenv globally.
sudo pip install virtualenvAnd use them by configuring our .profile.
For a given project, we first install a particular Python version
for it using the pyenv command (which must be done prior to
starting a new virtual environment with the mkvirtualenv command):
pyenv install 2.6.6 # pyenv versions to see what is installedGet path to this built/installed executable:
pyenv local 2.6.6
pyenv which pythonEach project (environment) will be named, and the version of
Python can be specified based on the results of the pyenv which
command above, for instance:
pyenv virtualenv 2.6.6 labAnd to use the environment in a shell, issue:
pyenv activate labOr, better yet, use the local option to make that environment
active whenever you enter that directory:
pyenv local labNow, commands like pip should be isolated to that virtual environment
as well as the needed version.
The Elpy Project deals with the virtualenvwrapper, so call
function, M-x pyvenv-workon, to activate a virtual environment,
however, we will want to check out pyenv-mode to see if will help.
WSGI files are just Python files in disguise, so tell them to use the Python environment:
(add-to-list 'auto-mode-alist '("\\.wsgi$" . python-mode))Careful with the tabs, my friend.
(setq-default indent-tabs-mode nil)
(setq-default tab-width 2)
(add-hook 'python-mode-hook '(lambda () (setq python-indent 4)))Need to color the defined variables:
(add-hook 'python-mode-hook 'color-identifiers-mode)That self business in Python is quite distracting.
(when (fboundp 'global-prettify-symbols-mode)
(add-hook 'python-mode-hook
(lambda ()
(push '("self" . ?◎) prettify-symbols-alist)
(modify-syntax-entry ?. "."))))My company has standardized on the pep8 project, just make sure you’ve
install the Flake8 library:
pyenv activate wpc # Or whatever the project name is
pip install --upgrade flake8Flycheck automatically supports Python with Flake8. To use it, set the virtual environment, and the errors should appear automatically.
Unit test and code coverage tool for Python now comes to Emacs with Python Nose.
(require 'nose nil t)Auto-completion system for Python. This code hooks Jedi into the standard Python mode. See these instructions for details (but this should have been installed for Elpy).
pip install jediNew keys:
C-Tabfor auto complete.C-.to jump to definition (overrides thectagsinterface)C-c dto show the function documentation
(when (require 'jedi nil t)
(add-hook 'python-mode-hook 'jedi:setup)
(add-hook 'python-mode-hook 'jedi:ac-setup)
(setq jedi:setup-keys t)
(setq jedi:complete-on-dot t))According to the ELPY Web Site, we first install the python-based package components:
# and importmagic for automatic imports
pip install importmagic
pip install elpyOnce this has been installed, we can enable it:
(when (require 'elpy nil t)
(elpy-enable))Make sure that we can simply require this library.
(provide 'init-python)Before you can build this on a new system, make sure that you put
the cursor over any of these properties, and hit: C-c C-c