Daanturo
Daanturo

Reputation: 134

Is there an Emacs Lisp API for Python interaction?

Something convenient like

(python-command-to-string &rest COMMANDS)

and the output from the Python interpreter is returned.

For example

(python-command-to-string
    "import jedi"
    "print(
jedi.Script(
code=\"[].append(0)\"
).infer(line=1, column=4)[0].full_name
)")

will return "builtins.list.append".

Using temporary .py files can get the job done but I'd love to have a reusable, common approach.

Upvotes: 1

Views: 246

Answers (1)

TerryTsao
TerryTsao

Reputation: 770

Ugly but kinda works. 😹

(defun python-command-to-string (&rest COMMANDS)
       "Use shell to call python."
       (shell-command-to-string
        (concat "python -c \"" (string-join COMMANDS "\n") "\"")))

(python-command-to-string
 "import numpy as np"
 "print(np.arange(6))"
 "print('blah blah')"
 "print('{}'.format(3))"
 )

Now there is the problem with using double quotes inside python code. I guess there is no harm with "single quotes only" constraints when invoking this function. Just don't upset the shell with stuff like ${}.

Upvotes: 1

Related Questions