spacemacs/layers/+lang/python/README.org

464 lines
21 KiB
Org Mode
Raw Normal View History

#+TITLE: Python layer
2015-06-10 16:44:30 +00:00
#+TAGS: general|layer|multi-paradigm|programming
2019-05-05 17:26:40 +00:00
[[file:img/python.png]]
2015-06-10 16:44:30 +00:00
2019-05-07 20:05:06 +00:00
* Table of Contents :TOC_5_gh:noexport:
2017-05-22 14:16:12 +00:00
- [[#description][Description]]
2017-08-28 21:58:10 +00:00
- [[#features][Features:]]
2017-05-22 14:16:12 +00:00
- [[#install][Install]]
- [[#layer][Layer]]
- [[#choosing-a-backend][Choosing a backend]]
- [[#backends][Backends]]
- [[#anaconda][Anaconda]]
- [[#language-server-protocol][Language Server Protocol]]
- [[#additional-tools][Additional tools]]
2018-05-08 22:19:21 +00:00
- [[#syntax-checking][Syntax checking]]
2017-05-22 14:16:12 +00:00
- [[#test-runner][Test runner]]
- [[#buffer-formatting][Buffer formatting]]
2017-05-22 14:16:12 +00:00
- [[#automatic-buffer-formatting-on-save][Automatic buffer formatting on save]]
- [[#automatic-save-of-buffer-when-testing][Automatic save of buffer when testing]]
2017-05-22 14:16:12 +00:00
- [[#autoflake][autoflake]]
- [[#pylookup][pylookup]]
- [[#dap-mode-debugger-only-for-lsp-backend][dap-mode debugger (only for lsp backend)]]
- [[#configuration][Configuration]]
- [[#fill-column][Fill column]]
- [[#sort-imports][Sort imports]]
- [[#importmagic][Importmagic]]
2017-05-22 14:16:12 +00:00
- [[#management-of-python-versions-and-virtual-environments][Management of Python versions and virtual environments]]
- [[#manage-virtual-environments-with-pyvenv][Manage virtual environments with pyvenv]]
- [[#manage-multiple-python-versions-with-pyenv][Manage multiple Python versions with pyenv]]
- [[#automatic-activation-of-local-pyenv-version][Automatic activation of local pyenv version]]
- [[#key-bindings][Key bindings]]
2017-05-22 14:16:12 +00:00
- [[#inferior-repl-process][Inferior REPL process]]
- [[#running-python-script-in-shell][Running Python Script in shell]]
- [[#testing][Testing]]
- [[#refactoring][Refactoring]]
- [[#pip-package-management][Pip package management]]
2017-05-22 14:16:12 +00:00
- [[#live-coding][Live coding]]
- [[#other-python-commands][Other Python commands]]
- [[#debugger][Debugger]]
2015-06-10 16:44:30 +00:00
* Description
This layer adds support for the Python language.
2017-08-28 21:58:10 +00:00
** Features:
- Support for the following backends:
- [[https://github.com/proofit404/anaconda-mode][anaconda]] (default),
- [[https://github.com/emacs-lsp/lsp-python][Language Server Protocol]] (experimental),
- Auto-completion
- Code Navigation
- Documentation Lookup using [[https://github.com/proofit404/anaconda-mode][anaconda-mode]] and [[https://github.com/tsgates/pylookup][pylookup]]
2015-06-10 16:44:30 +00:00
- Test Runners using [[https://github.com/syl20bnr/nose.el][nose.el]] or [[https://github.com/ionrock/pytest-el][pytest]]
- Virtual Environment using [[https://github.com/jorgenschaefer/pyvenv][pyvenv]] and [[https://github.com/yyuu/pyenv][pyenv]]
2015-06-10 16:44:30 +00:00
- semantic mode is enabled
- PEP8 compliant formatting via [[https://github.com/google/yapf][YAPF]] or [[https://github.com/ambv/black][black]]
- PEP8 checks with [[https://pypi.python.org/pypi/flake8][flake8]] or [[https://pypi.python.org/pypi/pylint/1.6.4][pylint]]
2015-06-10 16:44:30 +00:00
- Suppression of unused import with [[https://github.com/myint/autoflake][autoflake]]
- Use the ~%~ key to jump between blocks with [[https://github.com/redguardtoo/evil-matchit][evil-matchit]]
- Sort imports with [[https://pypi.python.org/pypi/isort][isort]]
- Fix a missing import statement with [[https://github.com/anachronic/importmagic.el][importmagic]]
- Pip package manager with [[https://github.com/brotzeit/pippel][pippel]]
2015-06-10 16:44:30 +00:00
* Install
2015-06-10 16:44:30 +00:00
** Layer
To use this configuration layer, add it to your =~/.spacemacs=. You will need to
add =python= to the existing =dotspacemacs-configuration-layers= list in this
file.
2015-06-10 16:44:30 +00:00
** Choosing a backend
To choose a default backend set the layer variable =python-backend=:
#+BEGIN_SRC elisp
2018-09-19 03:54:47 +00:00
(python :variables python-backend 'anaconda)
#+END_SRC
Backend can be chosen on a per project basis using directory local variables
(files named =.dir-locals.el= at the root of a project), an example to use the
=lsp= backend:
#+BEGIN_SRC elisp
2018-09-19 03:54:47 +00:00
;;; Directory Local Variables
;;; For more information see (info "(emacs) Directory Variables")
2018-09-19 03:54:47 +00:00
((python-mode (python-backend . lsp)))
#+END_SRC
*Note:* you can easily add a directory local variable with ~SPC f v d~.
* Backends
** Anaconda
=anaconda-mode= tries to install the dependencies itself but sometimes
it does not work and you may encounter the following message when
opening a python buffer:
2018-09-19 03:54:47 +00:00
#+BEGIN_EXAMPLE
Blocking call to accept-process-output with quit inhibited!!
#+END_EXAMPLE
To fix this, install the =anaconda-mode= [[https://github.com/proofit404/anaconda-mode/wiki][anaconda-deps]] by hand:
2018-09-19 03:54:47 +00:00
#+BEGIN_SRC sh
pip install --upgrade "jedi>=0.9.0" "json-rpc>=1.8.1" "service_factory>=0.1.5"
#+END_SRC
2017-06-12 10:38:27 +00:00
If you encounter problems with Jedi 1.0 consider downgrading to 0.9.0. See [[https://github.com/davidhalter/jedi/issues/873][this
issue]] for details.
2018-09-19 03:54:47 +00:00
Source: [[https://github.com/proofit404/anaconda-mode#issues]]
If you are facing errors such as "Unable to run anaconda-mode server", try
setting your =PYTHONPATH= as explained at
2018-09-19 03:54:47 +00:00
[[https://github.com/proofit404/anaconda-mode#pythonpath]]
** Language Server Protocol
You just have to install python language server package:
2018-09-19 03:54:47 +00:00
#+BEGIN_SRC sh
pip install python-language-server
#+END_SRC
Additionally you can install the following other packages:
2018-09-19 03:54:47 +00:00
#+BEGIN_SRC sh
# for import sorting
pip install pyls-isort
# for mypy checking (python 3.4+ is needed)
pip install pyls-mypy
#+END_SRC
If you've installed the language server and related packages as development
dependencies in a pipenv environment, you'll want to set the ~python-pipenv-activate~
config variable to ~t~. This activates your pipenv before enabling the
lsp backend.
* Additional tools
** Syntax checking
Syntax checking uses =flake8= package:
2018-09-19 03:54:47 +00:00
#+BEGIN_SRC sh
pip install flake8
#+END_SRC
2015-06-10 16:44:30 +00:00
** Test runner
Both =nose= and =pytest= are supported. By default =nose= is used.
To choose your test runner set the layer variable =python-test-runner= to
either =nose= or =pytest=.
#+BEGIN_SRC emacs-lisp
2018-09-19 03:54:47 +00:00
(setq-default dotspacemacs-configuration-layers
'((python :variables python-test-runner 'pytest)))
2015-06-10 16:44:30 +00:00
#+END_SRC
If you need both then you can set =python-test-runner= to a list like this:
2018-07-10 10:57:25 +00:00
#+BEGIN_SRC emacs-lisp
2018-09-19 03:54:47 +00:00
(setq-default dotspacemacs-configuration-layers
'((python :variables python-test-runner '(pytest nose))))
#+END_SRC
This means that =pytest= is your primary test runner. To use the secondary test
runner you can call the test functions with a prefix argument e.g. ~SPC u SPC m
t t~ to run one test with =nose=.
To set project specific test runners you can set =python-test-runner= in a
directory local variable in your project root. ~SPC f v d~ in Spacemacs. See
[[https://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html][the official documentation]] for more information.
The root of the project is detected with a =.git= directory or a =setup.cfg= file.
** Buffer formatting
One of [[https://github.com/google/yapf][YAPF]] (the default) or [[https://github.com/ambv/black][black]] may be selected as the formatter, via
=python-formatter=, as ='yapf= or ='black= respectively.
#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers '(
(python :variables python-formatter 'yapf)))
#+END_SRC
The key binding ~SPC m =~ invokes the selected formatter on the current buffer
when in python mode.
Note that YAPF and black may also be invoked unconditionally via
=yapfify-buffer= and =blacken-buffer=, respectively, provided that they are
installed.
2015-06-10 16:44:30 +00:00
** Automatic buffer formatting on save
To enable automatic buffer formatting on save set the variable
=python-format-on-save= to =t=. The formatter specified by =python-formatter=
will be used.
2015-06-10 16:44:30 +00:00
#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers '(
(python :variables python-format-on-save t)))
2015-06-10 16:44:30 +00:00
#+END_SRC
** Automatic save of buffer when testing
By default a buffer is automatically saved before tests are executed upon it,
you can disable this feature by setting =python-save-before-test= to =nil=.
#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers '(
(python :variables python-save-before-test nil)))
#+END_SRC
2015-06-10 16:44:30 +00:00
** autoflake
To be able to suppress unused imports easily, install [[https://github.com/myint/autoflake][autoflake]]:
#+BEGIN_SRC sh
pip install autoflake
#+END_SRC
** pylookup
To use =pylookup= on ~SPC m h H~, make sure you update the database first, using
~SPC SPC pylookup-update~.
** dap-mode debugger (only for lsp backend)
To use =dap-mode= for debugging do:
#+BEGIN_SRC bash
pip install "ptvsd>=4.2"
#+END_SRC
* Configuration
** Fill column
If you want to customize the fill column value, use something like this inside
the =user-init= function in your =.spacemacs=:
#+BEGIN_SRC elisp
2018-09-19 03:54:47 +00:00
(setq-default dotspacemacs-configuration-layers '(
(python :variables python-fill-column 99)))
#+END_SRC
** Sort imports
If you want imports to be automatically sorted when you save a file (using
[[https://pypi.python.org/pypi/isort][isort]]), set the =python-sort-imports-on-save= variable in the python layer
config section:
#+BEGIN_SRC elisp
2018-09-19 03:54:47 +00:00
(setq-default dotspacemacs-configuration-layers
'((python :variables python-sort-imports-on-save t)))
#+END_SRC
or as a directory-local variable (for per-project settings).
** Importmagic
Install importmagic and epc for importmagic functionality.
#+BEGIN_SRC sh
pip install importmagic epc
#+END_SRC
* Management of Python versions and virtual environments
** Manage virtual environments with pyvenv
A virtual environment provides isolation of your Python package versions. For a
general overview see [[http://docs.python-guide.org/en/latest/dev/virtualenvs/][this site]]. [[http://virtualenvwrapper.readthedocs.io/en/latest/index.html][Virtualenvwrapper]] which is also explained in the
previous link, is a program which manages your virtual environments in a central
location set by the =WORKON_HOME= environment variable.
Spacemacs integration of virtual environments and virtualenvwrapper is provided
by the [[https://github.com/jorgenschaefer/pyvenv][pyvenv]] package. It provides the following key bindings:
| Key binding | Description |
|-------------+-------------------------------------------------|
| ~SPC m V a~ | activate a virtual environment in any directory |
| ~SPC m V d~ | deactivate active virtual environment |
| ~SPC m V w~ | work on virtual environment in =WORKON_HOME= |
** Manage multiple Python versions with pyenv
If you need multiple Python versions (e.g. Python 2 and Python 3) then take a
look at [[https://github.com/yyuu/pyenv][pyenv]]. It enables the installation and managment of multiple
Python versions.
2016-08-24 18:27:57 +00:00
[[https://www.brianthicks.com/post/2015/04/15/automate-your-python-environment-with-pyenv/][This blogpost]] gives a good overview on how to use the tool. Spacemacs
integration is provided by [[https://github.com/proofit404/pyenv-mode][pyenv mode]] which has the following key bindings.
| Key binding | Description |
|-------------+--------------------------------------|
| ~SPC m v s~ | set a pyenv environment with [[https://github.com/pyenv/pyenv][pyenv]] |
| ~SPC m v u~ | unset a pyenv environment with [[https://github.com/pyenv/pyenv][pyenv]] |
Pyenv can also manage virtual environments for each of the Python versions it
has installed. Those will be listed alongside your Python versions.
*** Automatic activation of local pyenv version
A project-specific pyenv version may be written to a file called
=.python-version= using the [[https://github.com/yyuu/pyenv/blob/master/COMMANDS.md#user-content-pyenv-local][pyenv local]] command.
Spacemacs can search in parent directories for this file, and automatically set
the pyenv version. The behavior can be set with the variable
=python-auto-set-local-pyenv-version= to:
- =on-visit= (default) set the version when you visit a python buffer,
- =on-project-switch= set the version when you switch projects,
- =nil= to disable.
The same is also possible on pyvenv with a file called =.venv=. The behavior
can be set with the variable =python-auto-set-local-pyvenv-virtualenv== to:
- =on-visit= (default) set the virtualenv when you visit a python buffer,
- =on-project-switch= set the virtualenv when you switch projects,
- =nil= to disable.
* Key bindings
2015-06-10 16:44:30 +00:00
** Inferior REPL process
Start a Python or iPython inferior REPL process with ~SPC m s i~.
If =ipython= is available in system executable search paths, =ipython=
will be used to launch python shell; otherwise, default =python=
interpreter will be used. You may change your system executable
2015-06-10 16:44:30 +00:00
search path by activating a virtual environment.
Send code to inferior process commands:
| Key binding | Description |
2015-06-10 16:44:30 +00:00
|-------------+-------------------------------------------------|
| ~SPC m s b~ | send buffer and keep code buffer focused |
| ~SPC m s B~ | send buffer and switch to REPL in insert mode |
| ~SPC m s f~ | send function and keep code buffer focused |
| ~SPC m s F~ | send function and switch to REPL in insert mode |
| ~SPC m s i~ | start inferior REPL process |
| ~SPC m s r~ | send region and keep code buffer focused |
| ~SPC m s R~ | send region and switch to REPL in insert mode |
| ~CTRL+j~ | next item in REPL history |
| ~CTRL+k~ | previous item in REPL history |
** Running Python Script in shell
To run a Python script like you would in the shell press ~SPC m c c~
to start the Python script in comint mode. This is useful when working with
multiple Python files since the REPL does not reload changes made in other
modules.
| Key binding | Description |
2015-06-10 16:44:30 +00:00
|-------------+---------------------------------------------------------------------------|
| ~SPC m c c~ | Execute current file in a comint shell |
| ~SPC m c C~ | Execute current file in a comint shell and switch to it in =insert state= |
*Note:* With the universal argument ~SPC u~ you can enter a new
2015-06-10 16:44:30 +00:00
compilation command.
** Testing
Test commands start with ~m t~. To use the secondary test runner call the
function with a prefix argument, for example ~SPC u SPC m t a~.
2015-06-10 16:44:30 +00:00
| No Debug | Description |
|-------------+----------------------------------------------------------|
| ~SPC m t a~ | launch all tests of the project |
| ~SPC m t b~ | launch all tests of the current buffer (same as module) |
2018-02-24 20:57:58 +00:00
| ~SPC m t l~ | launch last tests |
2015-06-10 16:44:30 +00:00
| ~SPC m t m~ | launch all tests of the current module |
| ~SPC m t s~ | launch all tests of the current suite (only with =nose=) |
| ~SPC m t t~ | launch the current test (function) |
| Debug | Description |
|-------------+------------------------------------------------------------------------|
| ~SPC m t A~ | launch all tests of the project in debug mode |
| ~SPC m t B~ | launch all tests of the current buffer (module) in debug mode |
| ~SPC m t M~ | launch all tests of the current module in debug mode |
| ~SPC m t S~ | launch all tests of the current suite in debug mode (only with =nose=) |
| ~SPC m t T~ | launch the current test (function) in debug mode |
2015-06-10 16:44:30 +00:00
** Refactoring
| Key binding | Description |
|-------------+-------------------------------------------------|
| ~SPC m r f~ | fix a missing import statement with [[https://pypi.python.org/pypi/importmagic][importmagic]] |
| ~SPC m r i~ | remove unused imports with [[https://github.com/myint/autoflake][autoflake]] |
| ~SPC m r I~ | sort imports with [[https://pypi.python.org/pypi/isort][isort]] |
2015-06-10 16:44:30 +00:00
** Pip package management
In python buffer type ~SPC m P~ to open buffer listing all installed =pip=
packages in the currently activated virtual environment.
*Note:* To open this menu from outside a python buffer type
2018-09-19 03:54:47 +00:00
~SPC SPC pippel-list-packages RET~.
In the package list buffer:
| Key binding | Description |
|-------------+-------------------------------------------------------------|
| ~RET~ | follow link (=pippel-menu-visit-homepage=) |
| ~d~ | mark for deletion (=pippel-menu-mark-delete=) |
| ~i~ | prompt user for packages (=pippel-install-package=) |
| ~m~ | remove mark (=pippel-menu-mark-unmark=) |
| ~r~ | refresh package list (=pippel-list-packages=) |
| ~U~ | mark all upgradable (=pippel-menu-mark-all-upgrades=) |
| ~u~ | mark for upgrade (=pippel-menu-mark-upgrade=) |
| ~x~ | perform marked package menu actions (=pippel-menu-execute=) |
** Live coding
Live coding is provided by the [[https://github.com/donkirkby/live-py-plugin][live-py-plugin.]]
| Key binding | Description |
|-------------+---------------------|
| ~SPC m l~ | Toggle live-py-mode |
2015-06-10 16:44:30 +00:00
** Other Python commands
| Key binding | Description |
|---------------+-----------------------------------------------------------------------------------|
| ~SPC m =~ | reformat the buffer using formatter specified in =python-formatter= |
| ~SPC m d b~ | toggle a breakpoint using =wdb=, =ipdb=, =pudb=, =pdb= or =python3.7= (and above) |
| ~SPC m g a~ | go to assignment using =anaconda-mode-find-assignments= (~C-o~ to jump back) |
| ~SPC m g b~ | jump back |
| ~SPC m g g~ | go to definition using =anaconda-mode-find-definitions= (~C-o~ to jump back) |
| ~SPC m g u~ | navigate between usages with =anaconda-mode-find-references= |
| ~SPC m h d~ | look for documentation using =helm-pydoc= |
| ~SPC m h h~ | quick documentation using anaconda |
| ~SPC m h H~ | open documentation in =firefox= using [[https://github.com/tsgates/pylookup][pylookup]] |
| ~SPC m v a~ | activate a virtual environment in any directory |
| ~SPC m v d~ | deactivate active virtual environment |
| ~SPC m v s~ | set a pyenv environment with [[https://github.com/pyenv/pyenv][pyenv]] |
| ~SPC m v u~ | unset a pyenv environment with [[https://github.com/pyenv/pyenv][pyenv]] |
| ~SPC m v w~ | work on virtual environment in =WORKON_HOME= |
| ~SPC m v p a~ | activate pipenv in current project |
| ~SPC m v p d~ | deactivate pipenv in current project |
| ~SPC m v p i~ | install module into pipenv environment |
| ~SPC m v p o~ | open pipenv module in buffer |
| ~SPC m v p s~ | launch pipenv shell in current project |
| ~SPC m v p u~ | uninstall module from pipenv environment |
** Debugger
| Key binding | Description |
|---------------+---------------------------------|
| ~SPC m d d d~ | start debugging |
| ~SPC m d d l~ | debug last configuration |
| ~SPC m d d r~ | debug recent configuration |
|---------------+---------------------------------|
| ~SPC m d c~ | continue |
| ~SPC m d i~ | step in |
| ~SPC m d o~ | step out |
| ~SPC m d s~ | next step |
| ~SPC m d v~ | inspect value at point |
| ~SPC m d r~ | restart frame |
|---------------+---------------------------------|
| ~SPC m d .~ | debug transient state |
|---------------+---------------------------------|
| ~SPC m d a~ | abandon current session |
| ~SPC m d A~ | abandon all process |
|---------------+---------------------------------|
| ~SPC m d e e~ | eval |
| ~SPC m d e r~ | eval region |
| ~SPC m d e t~ | eval value at point |
|---------------+---------------------------------|
| ~SPC m d S s~ | switch session |
| ~SPC m d S t~ | switch thread |
| ~SPC m d S f~ | switch frame |
|---------------+---------------------------------|
| ~SPC m d I i~ | inspect |
| ~SPC m d I r~ | inspect region |
| ~SPC m d I t~ | inspect value at point |
|---------------+---------------------------------|
| ~SPC m d b b~ | toggle a breakpoint |
| ~SPC m d b c~ | change breakpoint condition |
| ~SPC m d b l~ | change breakpoint log condition |
| ~SPC m d b h~ | change breakpoint hit count |
| ~SPC m d b a~ | add a breakpoint |
| ~SPC m d b d~ | delete a breakpoint |
| ~SPC m d b D~ | clear all breakpoints |
|---------------+---------------------------------|
| ~SPC m d '_~ | Run debug REPL |
|---------------+---------------------------------|
| ~SPC m d w l~ | list local variables |
| ~SPC m d w o~ | goto output buffer if present |
| ~SPC m d w s~ | list sessions |
| ~SPC m d w b~ | list breakpoints |