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.

1 Answer 1


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.

  • This would report only the systems default, not the version at place inside a virtualenv maybe. Mar 13, 2017 at 13:42
  • @AndreasRöhler: not necessarily, please see note and the end.
    – sds
    Mar 13, 2017 at 13:54
  • There might exist links from python to python2 or 3. While inside Emacs source, by shebang or other customization, the default might be not selected. Mar 13, 2017 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.