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-virtualwrapper
Or, 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-virtualenv
Next, use pip to install virtualenv globally.
sudo pip install virtualenv
And 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 installed
Get path to this built/installed executable:
pyenv local 2.6.6
pyenv which python
Each 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 lab
And to use the environment in a shell, issue:
pyenv activate lab
Or, better yet, use the local
option to make that environment
active whenever you enter that directory:
pyenv local lab
Now, 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 4)
(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 ?. "."))))
According to the ELPY Web Site, we first install the python-based package components:
pip install jedi
# flake8 for code checks ... which installs pep8
pip install flake8
# and importmagic for automatic imports
pip install importmagic
pip install elpy
Once this has been installed, we can enable it:
(when (require 'elpy nil t)
(elpy-enable))
My company has standardized on the pep8
project, just make sure you’ve
install the Flake8 library:
workon wpc # Or whatever the project name is
pip install --upgrade flake8
Flycheck 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 jedi
New keys:
C-Tab
for auto complete.C-.
to jump to definition (overrides thectags
interface)C-c d
to 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))
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