[+lang/python] Poetry integration for python layer

Adding Poetry integration (tool for managing virtual environments in Python)
This commit is contained in:
Peter Kuchar 2020-12-21 21:59:28 +01:00 committed by Maximilian Wolff
parent 8f093ff2a3
commit 638bbb0c38
No known key found for this signature in database
GPG Key ID: 2DD07025BFDBD89A
4 changed files with 54 additions and 6 deletions

View File

@ -29,13 +29,14 @@
- [[#fill-column][Fill column]]
- [[#sort-imports][Sort imports]]
- [[#importmagic][Importmagic]]
- [[#pyvenv-pyenv-and-pipenv][Pyvenv, pyenv and pipenv]]
- [[#pyvenv-pyenv-pipenv-and-poetry][Pyvenv, pyenv, pipenv and poetry]]
- [[#management-of-python-versions-and-virtual-environments][Management of Python versions and virtual environments]]
- [[#manage-virtual-environments-with-pyvenv][Manage virtual environments with pyvenv]]
- [[#automatic-activation-of-local-virtual-environment][Automatic activation of local virtual environment]]
- [[#manage-multiple-python-versions-with-pyenv][Manage multiple Python versions with pyenv]]
- [[#automatic-activation-of-local-pyenv-version][Automatic activation of local pyenv version]]
- [[#manage-environments-and-packages-with-pipenv][Manage environments and packages with pipenv]]
- [[#manage-environments-and-packages-with-poetry][Manage environments and packages with Poetry]]
- [[#key-bindings][Key bindings]]
- [[#inferior-repl-process][Inferior REPL process]]
- [[#running-python-script-in-shell][Running Python Script in shell]]
@ -59,7 +60,7 @@ This layer adds support for the Python language.
- Code Navigation
- Documentation Lookup using [[https://github.com/proofit404/anaconda-mode][anaconda-mode]] and [[https://github.com/tsgates/pylookup][pylookup]]
- 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]] as well as [[https://github.com/pypa/pipenv][pipenv]]
- 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]]
- 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]]
@ -173,7 +174,7 @@ Additionally you can install the following other packages:
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.
lsp backend. The same applies for ~python-poetry-activate~.
*** Microsoft python language server
Paraphrasing the instructions provided by the author of the =lsp-python-ms= package:
@ -335,10 +336,10 @@ Install importmagic and epc for importmagic functionality.
pip install importmagic epc
#+END_SRC
** Pyvenv, pyenv and pipenv
** Pyvenv, pyenv, pipenv and poetry
Sometimes, it is convenient to be able to use python virtual environments from
other modes. For this reason, the python layer provides the variables
=spacemacs--python-pyenv-modes=, =spacemacs--python-pyvenv-modes= and
=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:
@ -429,6 +430,20 @@ It provides the following key bindings:
| ~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]] |
** 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:
| Key binding | Description |
|-------------+---------------------------------------------------------------------|
| ~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 |
* Key bindings
** Inferior REPL process
Start a Python or iPython inferior REPL process with ~SPC m s i~.

View File

@ -29,6 +29,9 @@ and `mspyls'")
(defvar python-pipenv-activate nil
"If non-nil, activate pipenv before enabling backend")
(defvar python-poetry-activate nil
"If non-nil, activate poetry before enabling backend")
(defvar python-formatter nil
"The formatter to use. Possible values are `yapf',
`black' and `lsp'.")
@ -74,6 +77,9 @@ Possible values are `on-visit', `on-project-switch' or `nil'.")
(defvar spacemacs--python-pipenv-modes nil
"List of major modes where to add pipenv support.")
(defvar spacemacs--python-poetry-modes nil
"List of major modes where to add poetry support.")
;; inferior-python-mode needs these variables to be defined. The python
;; package declares them but does not initialize them.
(defvar python-shell--interpreter nil)

View File

@ -25,9 +25,18 @@
((configuration-layer/layer-used-p 'lsp) 'lsp)
(t 'yapf))))
(defun spacemacs//poetry-activate ()
"Attempt to activate Poetry only if its configuration file is found."
(let ((root-path (locate-dominating-file default-directory "pyproject.toml")))
(when root-path
(message "Poetry configuration file found. Activating virtual environment.")
(poetry-venv-workon))
))
(defun spacemacs//python-setup-backend ()
"Conditionally setup python backend."
(when python-pipenv-activate (pipenv-activate))
(when python-pipenv-activate (pipenv-activate)
python-poetry-activate (spacemacs//poetry-activate))
(pcase (spacemacs//python-backend)
(`anaconda (spacemacs//python-setup-anaconda))
(`lsp (spacemacs//python-setup-lsp))))

View File

@ -29,6 +29,7 @@
org
pip-requirements
pipenv
poetry
pippel
py-isort
pyenv-mode
@ -216,6 +217,22 @@
"vps" 'pipenv-shell
"vpu" 'pipenv-uninstall)))))
(defun python/pre-init-poetry ()
(add-to-list 'spacemacs--python-poetry-modes 'python-mode))
(defun python/init-poetry ()
(use-package poetry
:defer t
:commands (poetry-venv-toggle
poetry-tracking-mode)
:init
(progn
(dolist (m spacemacs--python-poetry-modes)
(spacemacs/set-leader-keys-for-major-mode m
"vod" 'poetry-venv-deactivate
"vow" 'poetry-venv-workon
"vot" 'poetry-venv-toggle)))))
(defun python/init-pip-requirements ()
(use-package pip-requirements
:defer t))
@ -356,6 +373,7 @@
(spacemacs/declare-prefix-for-mode 'python-mode "mr" "refactor")
(spacemacs/declare-prefix-for-mode 'python-mode "mv" "virtualenv")
(spacemacs/declare-prefix-for-mode 'python-mode "mvp" "pipenv")
(spacemacs/declare-prefix-for-mode 'python-mode "mvo" "poetry")
(spacemacs/set-leader-keys-for-major-mode 'python-mode
"'" 'spacemacs/python-start-or-switch-repl
"cc" 'spacemacs/python-execute-file