Programmatically open an editor, capture the result.

fmoo, updated 🕥 2022-12-02 08:36:56

python-editor is a library that provides the editor module for programmatically interfacing with your system's $EDITOR.


python import editor commit_msg = editor.edit(contents=b"# Enter commit message here")

Opens an editor, prefilled with the contents, # Enter commit message here. When the editor is closed, returns the contents (bytes) in variable commit_msg. Note that the argument to contents needs to be a bytes object on Python 3.

python editor.edit(file="README.txt")

Opens README.txt in an editor. Changes are saved in place. If there is a contents argument then the file contents will be overwritten.

python editor.edit(..., use_tty=True)

Opens the editor in a TTY. This is usually done in programs which output is piped to other programs. In this case the TTY is used as the editor's stdout, allowing interactive usage.

How it Works

editor first looks for the ${EDITOR} environment variable. If set, it uses the value as-is, without fallbacks.

If no $EDITOR is set, editor will search through a list of known editors, and use the first one that exists on the system.

For example, on Linux, editor will look for the following editors in order:

  • vim
  • emacs
  • nano

When calling editor.edit, an editor will be opened in a subprocess, inheriting the parent process's stdin, stdout.


*** ERROR: ambiguous python shebang in python3.8/site-packages/ #!/usr/bin/env python. Change it to python3 (or python2) explicitly.

opened on 2023-01-25 10:49:36 by es-x

Hello when building packages rpmbuild get error

*** ERROR: ambiguous python shebang in /lib/python3.8/site-packages/ #!/usr/bin/env python. Change it to python3 (or python2) explicitly.

DeprecationWarning: The distutils package is deprecated

opened on 2023-01-24 15:34:55 by alexjuda

This line seems to trigger a deprecation warning on Python 3.10. This happens both for the latest released version (1.0.4) as well as the latest master.

Minimal working example

$ python -W default -c 'import editor; editor.get_editor()' /Users/alex/my-project/venv/lib/python3.10/site-packages/ DeprecationWarning: The distutils package is deprecated and slated for removal in Python 3.12. Use setuptools or check PEP 632 for potential alternatives from distutils.spawn import find_executable


  • Noisy test output from pytest test suite. Explanation: DeprecationWarnings are silenced by default but pytest shows them anyway. Projects that use editor get warnings in their test output.
  • This functionality will eventually break in Python 3.12.

Add a flag whether to read file

opened on 2022-12-02 08:36:55 by honnix

It is not always intended to read the file back, so adding a flag that users can decide.

editor can't handle args in editor environment variables

opened on 2021-06-09 18:36:00 by carlcosmos

common case is our users will have EDITOR set to gvim -f which causes editor to crash.

[noeol] 1L, 3C Error

opened on 2020-11-29 04:40:14 by rlgn

Trying to get a message doing the following:

msg = editor.edit(contents=b"PO# ")

instead the editor hangs and my Mac's fans kick in. It works when running as admin but right now I'm running in my non-admin desktop.

pytest: deprecation warning: imp module is deprecated.

opened on 2020-02-04 15:12:06 by dalf

When I use pytest, I have this warning: .../ve/lib/python3.6/distutils/ .../ve/lib/python3.6/distutils/ DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses import imp

It seems related to this line:

distutils imports imp.

shutil.which seems to be an alternative supported since Python 3.3.

Peter Ruibal
GitHub Repository