Stole Python package ideas from Gabriel Elanaro’s git project. The question is whether I use Rope or Jedi for auto-completion and or refactoring. 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.
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
(see these instructions).
WSGI files are just Python files in disguise, so tell them to use the Python environment. Careful with the tabs, my friend.
(use-package python
:mode ("\\.py\\'" . python-mode)
("\\.wsgi$" . python-mode)
:interpreter ("python" . python-mode)
:init
(setq-default indent-tabs-mode nil)
:config
(setq python-indent 4)
(add-hook 'python-mode-hook 'color-identifiers-mode))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.
pip install noseThe ELPY project automatically adds support for testing.
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 jediAccording 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:
(use-package elpy
:ensure t
:commands elpy-enable
:init (with-eval-after-load 'python (elpy-enable))
:config
(electric-indent-local-mode -1)
(delete 'elpy-module-highlight-indentation elpy-modules)
(delete 'elpy-module-flymake elpy-modules)
(defun ha/elpy-goto-definition ()
(interactive)
(condition-case err
(elpy-goto-definition)
('error (find-tag (symbol-name (symbol-at-point))))))
:bind (:map elpy-mode-map ([remap elpy-goto-definition] . ha/elpy-goto-definition)))Since ELPY is not a simple mode, but a collection of smaller modes
stitched together, we have to call with-eval-after-load (see this discussion)
See the documentation for details, but:
C-c C-f- Find Python file
C-c C-s- Grep for a Python symbol
C-c C-z- Switch to the Python Shell
C-c C-c- Send region to the Python interpreter
Note: The elpy-goto-definition is nice and all if you have a full project with a running interpreter, but I want to use tags as a fallback. However, since the function throws an error, I can’t simply advice the function, like:
(advice-add 'elpy-goto-definition :after-until 'find-tag)Instead, I had to create a function wrapper.
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