0

I need a function that return major version of "current" python interpreter. Possible choices are 2 or 3. How can I get it? Please, help me with this func.

Drew
  • 75,699
  • 9
  • 109
  • 225
sivakov512
  • 516
  • 3
  • 13

1 Answers1

4

Use shell-command-to-string and split-string:

(second (split-string (shell-command-to-string "python --version")))
==> "2.7.12+"

If you insist on a major version number, you can use string-to-number:

(string-to-number (first (split-string (second (split-string (shell-command-to-string "python --version"))) "\\.")))
==> 2

or (see string-match and match-string)

(let ((pv (shell-command-to-string "python --version")))
  (if (string-match "Python \\([0-9]+\\)\\." pv)
      (string-to-number (match-string 1 pv))
      (error "Unexpected `python --version` output [%s]" pv)))
==> 2

Note that in all cases you will get the version of whatever python executable found in the exec-path. IOW, normally this would be the system-wide /usr/bin/python, unless you started Emacs in a virtual environment or set it inside Emacs.

sds
  • 5,928
  • 20
  • 39