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.
Asked
Active
Viewed 140 times
1 Answers
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.
-
This would report only the systems default, not the version at place inside a virtualenv maybe. – Andreas Röhler Mar 13 '17 at 13:42
-
@AndreasRöhler: not necessarily, please see note and the end. – sds Mar 13 '17 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. – Andreas Röhler Mar 13 '17 at 15:30