2015-12-02 14:23:39 +00:00
#+TITLE : Python layer
2015-06-10 16:44:30 +00:00
2019-05-07 08:53:56 +00:00
#+TAGS : general|layer|multi-paradigm|programming
2019-05-05 17:26:40 +00:00
2015-07-04 03:52:33 +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 ]]
2018-02-19 05:08:27 +00:00
- [[#choosing-a-backend ][Choosing a backend ]]
- [[#backends ][Backends ]]
- [[#anaconda ][Anaconda ]]
- [[#language-server-protocol ][Language Server Protocol ]]
2021-05-13 22:30:32 +00:00
- [[#python-lsp-server ][python-lsp-server ]]
2019-01-28 10:47:03 +00:00
- [[#microsoft-python-language-server ][Microsoft python language server ]]
2020-09-01 15:01:27 +00:00
- [[#microsoft-pyright-language-server ][Microsoft pyright language server ]]
2018-02-19 05:08:27 +00:00
- [[#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 ]]
2018-12-30 17:30:40 +00:00
- [[#buffer-formatting ][Buffer formatting ]]
2017-05-22 14:16:12 +00:00
- [[#automatic-buffer-formatting-on-save ][Automatic buffer formatting on save ]]
2018-01-06 02:41:26 +00:00
- [[#automatic-save-of-buffer-when-testing ][Automatic save of buffer when testing ]]
2017-05-22 14:16:12 +00:00
- [[#autoflake ][autoflake ]]
- [[#pylookup ][pylookup ]]
2019-04-18 19:16:31 +00:00
- [[#dap-mode-debugger-only-for-lsp-backend ][dap-mode debugger (only for lsp backend) ]]
2018-01-06 03:15:50 +00:00
- [[#configuration ][Configuration ]]
- [[#fill-column ][Fill column ]]
- [[#sort-imports ][Sort imports ]]
2019-04-16 21:55:16 +00:00
- [[#importmagic ][Importmagic ]]
2020-12-21 20:59:28 +00:00
- [[#pyvenv-pyenv-pipenv-and-poetry ][Pyvenv, pyenv, pipenv and poetry ]]
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 ]]
2020-09-07 15:19:14 +00:00
- [[#automatic-activation-of-local-virtual-environment ][Automatic activation of local virtual environment ]]
2017-05-22 14:16:12 +00:00
- [[#manage-multiple-python-versions-with-pyenv ][Manage multiple Python versions with pyenv ]]
- [[#automatic-activation-of-local-pyenv-version ][Automatic activation of local pyenv version ]]
2019-11-16 16:51:49 +00:00
- [[#manage-environments-and-packages-with-pipenv ][Manage environments and packages with pipenv ]]
2020-12-21 20:59:28 +00:00
- [[#manage-environments-and-packages-with-poetry ][Manage environments and packages with Poetry ]]
2018-12-05 03:03:03 +00:00
- [[#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 ]]
2018-01-06 03:15:50 +00:00
- [[#pip-package-management ][Pip package management ]]
2017-05-22 14:16:12 +00:00
- [[#live-coding ][Live coding ]]
- [[#other-python-commands ][Other Python commands ]]
2019-04-14 10:15:21 +00:00
- [[#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:
2018-02-19 05:08:27 +00:00
- Support for the following backends:
2019-12-15 12:56:49 +00:00
- [[https://github.com/proofit404/anaconda-mode ][anaconda ]] (default),
2020-10-03 18:48:37 +00:00
- [[https://github.com/emacs-lsp/lsp-python-ms ][Language Server Protocol ]] (experimental - 2 implementations),
2021-05-13 22:30:32 +00:00
- python-lsp-server
2019-01-28 10:47:03 +00:00
- Microsoft python language server
2018-02-19 05:08:27 +00:00
- Auto-completion
- Code Navigation
2018-10-23 14:18:21 +00:00
- 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 ]]
2020-12-21 20:59:28 +00:00
- Virtual Environment using [[https://github.com/jorgenschaefer/pyvenv ][pyvenv ]] and [[https://github.com/yyuu/pyenv ][pyenv ]] as well as [[https://github.com/pypa/pipenv ][pipenv ]] and [[https://github.com/galaunay/poetry.el ][poetry ]]
2015-06-10 16:44:30 +00:00
- semantic mode is enabled
2018-12-30 17:30:40 +00:00
- PEP8 compliant formatting via [[https://github.com/google/yapf ][YAPF ]] or [[https://github.com/ambv/black ][black ]]
2016-08-16 18:06:45 +00:00
- 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 ]]
2015-09-21 00:56:37 +00:00
- Use the ~%~ key to jump between blocks with [[https://github.com/redguardtoo/evil-matchit ][evil-matchit ]]
2015-11-07 08:28:26 +00:00
- Sort imports with [[https://pypi.python.org/pypi/isort ][isort ]]
2017-12-04 11:30:55 +00:00
- Fix a missing import statement with [[https://github.com/anachronic/importmagic.el ][importmagic ]]
2018-01-06 03:15:50 +00:00
- Pip package manager with [[https://github.com/brotzeit/pippel ][pippel ]]
2019-09-30 01:30:53 +00:00
- Interactive debugger using [[https://github.com/emacs-lsp/dap-mode ][dap-mode ]]
2015-06-10 16:44:30 +00:00
2015-10-30 11:20:58 +00:00
* Install
2015-06-10 16:44:30 +00:00
** Layer
2016-01-06 05:21:55 +00:00
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
2018-02-19 05:08:27 +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)
2018-02-19 05:08:27 +00:00
#+END_SRC
2019-12-15 12:56:49 +00:00
Alternatively the =lsp= backend will be automatically chosen if the layer =lsp=
is used and you did not specify any value for =python-backend= .
Backend can be chosen on a per project basis using directory local variables
2018-02-19 05:08:27 +00:00
(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-02-19 05:08:27 +00:00
2018-09-19 03:54:47 +00:00
((python-mode (python-backend . lsp)))
2018-02-19 05:08:27 +00:00
#+END_SRC
*Note:* you can easily add a directory local variable with ~SPC f v d~ .
2019-01-28 10:47:03 +00:00
The available options are:
2021-05-13 22:51:36 +00:00
| symbol | description |
|-----------+---------------------------|
| 'anaconda | Default |
2021-05-13 22:30:32 +00:00
| 'lsp | python-lsp-server package |
2019-01-28 10:47:03 +00:00
2018-02-19 05:08:27 +00:00
* Backends
** Anaconda
2016-10-17 03:03:40 +00:00
=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
2016-10-17 03:03:40 +00:00
2016-12-02 03:22:39 +00:00
To fix this, install the =anaconda-mode= [[https://github.com/proofit404/anaconda-mode/wiki ][anaconda-deps ]] by hand:
2016-10-17 03:03:40 +00:00
2018-09-19 03:54:47 +00:00
#+BEGIN_SRC sh
2018-11-04 16:41:32 +00:00
pip install --upgrade "jedi>=0.13.0" "json-rpc>=1.8.1" "service_factory>=0.1.5"
2018-09-19 03:54:47 +00:00
#+END_SRC
2016-10-17 03:03:40 +00:00
2018-11-04 16:41:32 +00:00
If you encounter problems with Jedi 1.0 consider downgrading to 0.13.0. See [[https://github.com/davidhalter/jedi/issues/873][this
2017-06-12 10:38:27 +00:00
issue]] for details.
2018-09-19 03:54:47 +00:00
Source: [[https://github.com/proofit404/anaconda-mode#issues ]]
2016-10-17 03:03:40 +00:00
2019-05-26 20:58:52 +00:00
If you are facing errors such as "Unable to run anaconda-mode server", try
2016-11-22 20:53:50 +00:00
setting your =PYTHONPATH= as explained at
2018-09-19 03:54:47 +00:00
[[https://github.com/proofit404/anaconda-mode#pythonpath ]]
2016-10-17 03:03:40 +00:00
2018-02-19 05:08:27 +00:00
** Language Server Protocol
2019-01-28 10:47:03 +00:00
The =lsp= backend can use either of the following language server implementations:
2021-05-13 22:30:32 +00:00
| symbol | description |
|----------+-------------------------------------|
| 'pylsp | [[https://pypi.org/project/python-lsp-server/][python-lsp-server package]] (default) |
| 'mspyls | [[https://github.com/emacs-lsp/lsp-python-ms][Microsoft python language server]] |
| 'pyright | [[https://github.com/emacs-lsp/lsp-pyright][Microsoft pyright language server]] |
2019-01-28 10:47:03 +00:00
2021-05-13 22:30:32 +00:00
=pylsp= is used by default - to use the Microsoft python language server, set the
2020-09-01 15:01:27 +00:00
=python-lsp-server= layer variable as follows:
2019-01-28 10:47:03 +00:00
#+BEGIN_SRC elisp
(python :variables python-backend 'lsp python-lsp-server 'mspyls)
#+END_SRC
2020-09-01 15:01:27 +00:00
To setup the pyright language server instead, use:
2020-09-04 16:48:12 +00:00
2020-09-01 15:01:27 +00:00
#+BEGIN_SRC elisp
(python :variables python-backend 'lsp python-lsp-server 'pyright)
#+END_SRC
2021-05-13 22:30:32 +00:00
*** python-lsp-server
2018-02-19 05:08:27 +00:00
You just have to install python language server package:
2018-09-19 03:54:47 +00:00
#+BEGIN_SRC sh
2021-05-05 17:28:52 +00:00
pip install 'python-language-server[all]'
2018-09-19 03:54:47 +00:00
#+END_SRC
2018-02-19 05:08:27 +00:00
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
2021-05-14 17:36:11 +00:00
# for formating with black
2019-12-15 12:56:49 +00:00
pip install pyls-black
2021-05-14 17:36:11 +00:00
# for detecting the use of deprecated apis
pip install pyls-memestra
2018-09-19 03:54:47 +00:00
#+END_SRC
2018-02-19 05:08:27 +00:00
2019-05-26 20:58:52 +00:00
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~
2018-12-11 21:59:25 +00:00
config variable to ~t~ . This activates your pipenv before enabling the
2020-12-21 20:59:28 +00:00
lsp backend. The same applies for ~python-poetry-activate~ .
2018-12-11 21:59:25 +00:00
2019-01-28 10:47:03 +00:00
*** Microsoft python language server
Paraphrasing the instructions provided by the author of the =lsp-python-ms= package:
#+BEGIN_SRC sh
git clone https://github.com/Microsoft/python-language-server.git
cd python-language-server/src/LanguageServer/Impl
dotnet build -c Release
dotnet publish -c Release -r <RUNTIME >
#+END_SRC
where ~<RUNTIME>~ is one of the [[https://docs.microsoft.com/en-us/dotnet/core/rid-catalog ][runtime IDs supported by dotnet core ]]. One of ~linux-x64~ , ~osx-x64~ , ~win10-x64~ should
cover most use cases.
The default package configuration assumes the executable is located in a folder included in your system path.
To use the latest built version in a cloned git repo, use the ~python-lsp-git-root~ config variable, e.g.:
#+BEGIN_SRC elisp
(setq-default dotspacemacs-configuration-layers
'((python :variables
2019-06-15 17:24:06 +00:00
python-backend 'lsp
python-lsp-server 'mspyls
2019-01-28 10:47:03 +00:00
python-lsp-git-root "~/dev/python/python-language-server")))
#+END_SRC
N.B. If you're using Arch linux or a derivative distribution, you can install the =microsoft-python-language-server=
package from the AUR.
2020-09-01 15:01:27 +00:00
*** Microsoft pyright language server
[[https://github.com/microsoft/pyright ][Pyright ]] is a new language server by Microsoft rewritten from scratch. Microsoft
python language server is planned to be deprecated in favor of pyright. Pyright
offers improved performance and better features compared to the old
implementation. It can be installed via yarn or npm as follows:
#+BEGIN_SRC sh
# via yarn
yarn global add pyright
# or via npm
npm install -g pyright
#+END_SRC
2018-02-19 05:08:27 +00:00
* Additional tools
2018-04-15 19:13:20 +00:00
** Syntax checking
2016-10-24 18:53:14 +00:00
Syntax checking uses =flake8= package:
2016-10-17 03:03:40 +00:00
2018-09-19 03:54:47 +00:00
#+BEGIN_SRC sh
pip install flake8
#+END_SRC
2016-10-17 03:03:40 +00:00
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
2016-04-13 21:50:53 +00:00
If you need both then you can set =python-test-runner= to a list like this:
2018-07-10 10:57:25 +00:00
2016-04-13 21:50:53 +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))))
2016-04-13 21:50:53 +00:00
#+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.
2018-12-30 17:30:40 +00:00
** Buffer formatting
2019-12-15 12:56:49 +00:00
One of [[https://github.com/google/yapf ][YAPF ]] (the default), [[https://github.com/ambv/black ][black ]] or =lsp= may be selected as the formatter, via
2020-07-29 06:37:42 +00:00
=python-formatter= , as =yapf= , =black= or =lsp= respectively.
2018-12-30 17:30:40 +00:00
#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers '(
(python :variables python-formatter 'yapf)))
#+END_SRC
2019-12-15 12:56:49 +00:00
Alternatively the =lsp= formatter will be automatically chosen if the layer =lsp=
is used and you did not specify any value for =python-formatter= .
2018-12-30 17:30:40 +00:00
The key binding ~SPC m =~ invokes the selected formatter on the current buffer
2019-08-31 21:21:03 +00:00
when in non LSP python mode otherwise ~SPC m ==~ is used.
2018-12-30 17:30:40 +00:00
2019-08-31 21:21:03 +00:00
Note that a specific formatter may also be invoked unconditionally via
2019-08-31 21:42:39 +00:00
=yapfify-buffer= , =blacken-buffer= or =lsp-format-buffer= , provided
2019-08-31 21:21:03 +00:00
these are installed.
2018-12-30 17:30:40 +00:00
2015-06-10 16:44:30 +00:00
** Automatic buffer formatting on save
2018-12-30 17:30:40 +00:00
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 '(
2018-12-30 17:30:40 +00:00
(python :variables python-format-on-save t)))
2015-06-10 16:44:30 +00:00
#+END_SRC
2018-01-06 02:41:26 +00:00
** 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
2015-10-06 10:18:33 +00:00
** pylookup
To use =pylookup= on ~SPC m h H~ , make sure you update the database first, using
2016-02-07 13:39:54 +00:00
~SPC SPC pylookup-update~ .
2015-10-06 10:18:33 +00:00
2018-12-30 17:30:40 +00:00
** dap-mode debugger (only for lsp backend)
2019-04-14 10:15:21 +00:00
To use =dap-mode= for debugging do:
2019-04-15 20:21:30 +00:00
2019-04-14 10:15:21 +00:00
#+BEGIN_SRC bash
pip install "ptvsd>=4.2"
#+END_SRC
2019-04-15 20:21:30 +00:00
2018-01-06 03:15:50 +00:00
* 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)))
2018-01-06 03:15:50 +00:00
#+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)))
2018-01-06 03:15:50 +00:00
#+END_SRC
or as a directory-local variable (for per-project settings).
2019-04-16 21:55:16 +00:00
** Importmagic
2019-04-17 13:48:54 +00:00
Install importmagic and epc for importmagic functionality.
2019-04-16 21:55:16 +00:00
#+BEGIN_SRC sh
pip install importmagic epc
#+END_SRC
2020-12-21 20:59:28 +00:00
** Pyvenv, pyenv, pipenv and poetry
2019-11-16 16:51:49 +00:00
Sometimes, it is convenient to be able to use python virtual environments from
other modes. For this reason, the python layer provides the variables
2020-12-30 20:55:27 +00:00
=spacemacs--python-pyenv-modes= , =spacemacs--python-pyvenv-modes= ,
=spacemacs--python-poetry-modes= and =spacemacs--python-pipenv-modes= .
If you wish to be able to access these functionalities from other modes,
in your user config section, do:
2019-11-16 16:51:49 +00:00
#+BEGIN_SRC elisp
(add-to-list 'spacemacs--python-pipenv-mode 'your-mode)
#+END_SRC
This will allow you to use [[https://github.com/pwalsh/pipenv.el ][pipenv ]] bindings from the mode your-mode.
You can add to the other two lists analogously.
2019-11-23 21:43:58 +00:00
2016-01-06 19:48:18 +00:00
* Management of Python versions and virtual environments
** Manage virtual environments with pyvenv
A virtual environment provides isolation of your Python package versions. For a
2017-04-26 13:50:19 +00:00
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
2016-01-06 19:48:18 +00:00
previous link, is a program which manages your virtual environments in a central
2016-11-22 20:53:50 +00:00
location set by the =WORKON_HOME= environment variable.
2016-01-06 19:48:18 +00:00
Spacemacs integration of virtual environments and virtualenvwrapper is provided
2018-12-05 03:03:03 +00:00
by the [[https://github.com/jorgenschaefer/pyvenv ][pyvenv ]] package. It provides the following key bindings:
2016-01-06 19:48:18 +00:00
2018-12-05 03:03:03 +00:00
| Key binding | Description |
2016-01-06 19:48:18 +00:00
|-------------+-------------------------------------------------|
2021-06-16 16:14:01 +00:00
| ~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= |
2016-01-06 19:48:18 +00:00
2020-09-07 15:19:14 +00:00
*** Automatic activation of local virtual environment
By default Spacemacs uses the [[https://github.com/jorgenschaefer/pyvenv ][pyvenv ]] package to manage virtual environments.
Additionally it uses =pyvenv-tracking-mode= to activate a buffer's local virtual
environment on change of focus. Pyvenv determines which virtual environment to
use from the value of the =pyvenv-workon= or the =pyvenv-activate=
buffer-local-variable. Spacemacs scans the project directory for a pattern
=.venv= . If the found =.venv= is a directory then it sets that directory as the
local virtual environment path. If the =.venv= pattern is a file then it checks if
its first line matches an existing path and if so, it sets it as the local
virtual environment path. Finally it checks if it finds an existing directory
with the name of the first line in the ~pyvenv-workon-home~ directory. By default
Spacemacs scans for a virtual environment and sets the local =pyvenv-workon= or
the =pyvenv-activate= variables on visiting a file, but switches virtual
environment on every change of focus using the local variables. The buffer
tracking behavior can be disabled by setting the value of the customizable
variable =pyvenv-tracking-mode= to =nil= . The scanning behavior can be set via 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.
2020-09-14 18:13:15 +00:00
2016-01-06 19:48:18 +00:00
** Manage multiple Python versions with pyenv
If you need multiple Python versions (e.g. Python 2 and Python 3) then take a
2019-07-15 20:32:52 +00:00
look at [[https://github.com/yyuu/pyenv ][pyenv ]]. It enables the installation and management of multiple
2016-01-06 19:48:18 +00:00
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
2018-12-05 03:03:03 +00:00
integration is provided by [[https://github.com/proofit404/pyenv-mode ][pyenv mode ]] which has the following key bindings.
2016-01-06 19:48:18 +00:00
2018-12-05 03:03:03 +00:00
| Key binding | Description |
2016-01-06 19:48:18 +00:00
|-------------+--------------------------------------|
2017-04-26 13:50:19 +00:00
| ~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]] |
2016-01-06 19:48:18 +00:00
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
2017-04-26 13:50:19 +00:00
=.python-version= using the [[https://github.com/yyuu/pyenv/blob/master/COMMANDS.md#user-content-pyenv-local ][pyenv local ]] command.
2016-01-06 19:48:18 +00:00
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,
2016-10-25 17:25:57 +00:00
- =nil= to disable.
2019-11-16 16:51:49 +00:00
** Manage environments and packages with pipenv
[[https://pipenv.kennethreitz.org/en/latest/ ][Pipenv ]] is the new standard tool to manage your virtual environments. It can act as
a replacement for both =pyenv= and =venv= on a per-repository basis. An overview
of how to use the tool is provided [[https://pipenv.kennethreitz.org/en/latest/basics/ ][here ]].
Spacemacs integration for pipenv is provided by the [[https://github.com/pwalsh/pipenv.el ][pipenv package ]].
It provides the following key bindings:
| Key binding | Description |
|---------------+------------------------------------------------------------|
| ~SPC m v p a~ | activate a pipenv environment with [[https://github.com/pwalsh/pipenv.el][pipenv]] |
| ~SPC m v p d~ | deactivate a pipenv environment with [[https://github.com/pwalsh/pipenv.el][pipenv]] |
| ~SPC m v p i~ | install a package into a virtual environment with [[https://github.com/pwalsh/pipenv.el][pipenv]] |
| ~SPC m v p o~ | open an installed module in a new buffer with [[https://github.com/pwalsh/pipenv.el][pipenv]] |
| ~SPC m v p s~ | open a shell buffer in the current environment with [[https://github.com/pwalsh/pipenv.el][pipenv]] |
| ~SPC m v p u~ | uninstall a package from a virtual environment with [[https://github.com/pwalsh/pipenv.el][pipenv]] |
2020-12-21 20:59:28 +00:00
** Manage environments and packages with Poetry
Poetry is a tool for dependency management and packaging in Python.
It allows you to declare the libraries your project depends on and it will
manage (install/update) them for you.
Spacemacs integration for Poetry is provided by the [[https://github.com/galaunay/poetry.el ][poetry ]] package.
It provides the following key bindings:
2020-12-30 21:04:48 +00:00
| Key binding | Description |
|---------------+---------------------------------------------------------------------|
2020-12-21 20:59:28 +00:00
| ~SPC m v o d~ | De-activate the virtualenv associated to the current poetry project |
| ~SPC m v o a~ | Activate the virtualenv associated to the current poetry project |
| ~SPC m v o t~ | Toggle the virtualenv associated to the current poetry project |
2018-12-05 03:03:03 +00:00
* 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=
2018-10-23 14:18:21 +00:00
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:
2021-08-02 14:56:32 +00:00
| Key binding | Description |
|-------------+--------------------------------------------------------------|
| ~SPC m s s~ | send region (or line when region not active) and show output |
| ~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 e~ | send statement and keep code buffer focused |
| ~SPC m s E~ | send statement 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 l~ | send line and keep code buffer focused |
| ~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 |
2015-06-10 16:44:30 +00:00
** 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.
2018-12-05 03:03:03 +00:00
| 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= |
2015-06-10 21:16:01 +00:00
*Note:* With the universal argument ~SPC u~ you can enter a new
2015-06-10 16:44:30 +00:00
compilation command.
** Testing
2016-04-13 21:50:53 +00:00
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 |
|-------------+------------------------------------------------------------------------|
2015-08-25 01:23:48 +00:00
| ~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
2018-12-05 03:03:03 +00:00
| Key binding | Description |
2017-12-04 11:30:55 +00:00
|-------------+-------------------------------------------------|
2018-01-06 02:45:57 +00:00
| ~SPC m r f~ | fix a missing import statement with [[https://pypi.python.org/pypi/importmagic][importmagic]] |
2017-12-04 11:30:55 +00:00
| ~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
2018-01-06 03:15:50 +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~ .
2018-01-06 03:15:50 +00:00
In the package list buffer:
2018-12-05 03:03:03 +00:00
| Key binding | Description |
2018-01-06 03:15:50 +00:00
|-------------+-------------------------------------------------------------|
| ~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=) |
2015-10-29 08:42:37 +00:00
** Live coding
Live coding is provided by the [[https://github.com/donkirkby/live-py-plugin ][live-py-plugin. ]]
2018-12-05 03:03:03 +00:00
| Key binding | Description |
2015-10-29 08:42:37 +00:00
|-------------+---------------------|
2018-12-22 11:04:49 +00:00
| ~SPC m l~ | Toggle live-py-mode |
2015-10-29 08:42:37 +00:00
2015-06-10 16:44:30 +00:00
** Other Python commands
2019-08-31 21:21:03 +00:00
| Key binding | Description |
|--------------------------+-----------------------------------------------------------------------------------|
| ~SPC m =~ or ~SPC m = =~ | reformat the buffer using default 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]] |
2020-09-28 13:50:50 +00:00
| ~SPC m S d~ | insert doc-string skeleton using [[https://github.com/naiquevin/sphinx-doc.el][sphinx-doc]] |
2019-08-31 21:21:03 +00:00
| ~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 |
2019-04-15 20:21:30 +00:00
2019-04-14 10:15:21 +00:00
** Debugger
2019-09-30 01:30:53 +00:00
Using the =dap= layer you'll get access to all the DAP key bindings, see the
complete list of key bindings on the [[https://github.com/syl20bnr/spacemacs/tree/develop/layers/%2Btools/dap#key-bindings ][dap layer description ]].