Yaroslav Bulatov
Yaroslav Bulatov

Reputation: 57893

Custom Python shell for Emacs python-mode

What does it take to replace regular /usr/bin/python with a custom python shell in Emacs' python-mode?

Basically I have a binary /usr/bin/mypython which does some initializations before starting python interpreter prompt, and for all interaction purposes, the resulting interpreter shell is equivalent to /usr/bin/python.

However, if I specify this binary in "python-python-command" (using python.el in Emacs 23.3), I get "Only Python versions >=2.2 and < 3.0 are supported"

Upvotes: 6

Views: 1395

Answers (3)

Andreas R&#246;hler
Andreas R&#246;hler

Reputation: 4804

with python-mode.el just do

M-x MY-PYTHON RET

given the Python version is installed

https://launchpad.net/python-mode

Upvotes: 0

Charlie Martin
Charlie Martin

Reputation: 112356

I'm about to read the elisp to check, but I'd bet if you added a --version flag that gave the save results as /usr/bin/python, emacs would be happy.

Update

Here's the code in python.el line 1555 et seq in EMACS 23.3.1:

(defvar python-version-checked nil)
(defun python-check-version (cmd)
  "Check that CMD runs a suitable version of Python."
  ;; Fixme:  Check on Jython.
  (unless (or python-version-checked
          (equal 0 (string-match (regexp-quote python-python-command)
                     cmd)))
    (unless (shell-command-to-string cmd)
      (error "Can't run Python command `%s'" cmd))
    (let* ((res (shell-command-to-string
                 (concat cmd
                         " -c \"from sys import version_info;\
print version_info >= (2, 2) and version_info < (3, 0)\""))))
      (unless (string-match "True" res)
    (error "Only Python versions >= 2.2 and < 3.0 are supported")))
    (setq python-version-checked t)))

What it's doing is running a one-liner

from sys import version_info;
print version_info >= (2, 2) and version_info < (3, 0)

that just prints "True" or "False". Fix your script to handle the -c flag and you should be fine.

Alternatively, you could take the hacker's way out and force the value of python-version-checked to t, and it'll never do the check.

Upvotes: 5

cjm
cjm

Reputation: 62089

The easiest way to defeat the check is to lie and tell python-check-version that it's already checked:

(setq python-version-checked t)

Upvotes: 1

Related Questions