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.
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:
When calling editor.edit
, an editor will be opened in a subprocess, inheriting
the parent process's stdin, stdout.
Hello when building packages rpmbuild get error
*** ERROR: ambiguous python shebang in /lib/python3.8/site-packages/editor.py: #!/usr/bin/env python. Change it to python3 (or python2) explicitly.
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
.
$ python -W default -c 'import editor; editor.get_editor()'
/Users/alex/my-project/venv/lib/python3.10/site-packages/editor.py:11: 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
pytest
test suite. Explanation: DeprecationWarnings
are silenced by default but pytest
shows them anyway. Projects that use editor
get warnings in their test output.It is not always intended to read the file back, so adding a flag that users can decide.
common case is our users will have EDITOR set to gvim -f
which causes editor to crash.
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.
When I use pytest, I have this warning:
.../ve/lib/python3.6/distutils/__init__.py:4
.../ve/lib/python3.6/distutils/__init__.py:4: 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: https://github.com/fmoo/python-editor/blob/d9c95d5a1b1824fb58b16b7edeacdc5cad896e90/editor.py#L11
distutils imports imp.
shutil.which seems to be an alternative supported since Python 3.3.