How to specify the default interpreter

Asked by DougRenze

How do I specify the default interpreter used by python-mode? On my system, I have both python2 and python3 installed. If I am executing a script in python-mode that doesn't have a shebang line, python-mode always executes it using the python2 interpreter (which is what /bin/python is symlinked to). However, I am almost always writing python3 code. Thanks.

Question information

Language:
English Edit question
Status:
Solved
For:
python-mode.el Edit question
Assignee:
No assignee Edit question
Solved by:
Andreas Roehler
Solved:
Last query:
Last reply:
Revision history for this message
Best Andreas Roehler (a-roehler) said :
#1

On 19.06.2014 04:37, DougRenze wrote:

[ ... ]

M-x customize-variable RET py-python-command RET

Edit string according to Python-version existing in $PATH.

Revision history for this message
DougRenze (drenze) said :
#2

Thanks Andreas Roehler, that solved my question.

Revision history for this message
DougRenze (drenze) said :
#3

A follow-up, which shows my ignorance of Emacs.

I put the following in my `init.el` and restarted:

    (setq py-python-command "/usr/bin/python3")

...but when I restarted Emacs, examining the contents of `py-python-command` still show it as having a value of `python`. What am I doing wrong?

Thanks.

Revision history for this message
Andreas Roehler (a-roehler) said :
#4

On 20.06.2014 00:56, DougRenze wrote:
> Question #250443 on python-mode.el changed:
> https://answers.launchpad.net/python-mode/+question/250443
>
> DougRenze posted a new comment:
> A follow-up, which shows my ignorance of Emacs.
>
> I put the following in my `init.el` and restarted:
>
> (setq py-python-command "/usr/bin/python3")
>
> ...but when I restarted Emacs, examining the contents of `py-python-
> command` still show it as having a value of `python`. What am I doing
> wrong?
>
> Thanks.
>

Looks like a bug. Please file it again as a bug-report.

Thanks,

Andreas

Revision history for this message
Andreas Roehler (a-roehler) said :
#5

Fixed several bugs around.

WRT specifying a different default via init,
the following proceeding should work:

Erase related customization from defcustom.

Use py-shell-name - py-python-command is just an alias.
Use `setq-default' - it's a buffer-local variable, `setq' only affects the current buffer.

For example
(setq-default py-shell-name "/usr/bin/python2.7")

Open buffers with a specific value:

(with-current-buffer
    (get-buffer-create "test.py")
  (setq py-shell-name "/usr/bin/python2"))

Revision history for this message
DougRenze (drenze) said :
#6

Hmm.

(setq-default py-shell-name "/usr/bin/python3")

doesn't seem to do the trick either, but

(custom-set-variables '(py-shell-name "/usr/bin/python3"))

does.

Revision history for this message
DougRenze (drenze) said :
#7

I should also note that after including

    (setq-default py-shell-name "/usr/bin/python3")

in the `init.el`, restarting and entering the following into the `*scratch*` buffer:

    (print py-shell-name)

generates

    "/usr/bin/python3"
    "/usr/bin/python3"

for output. However, doing C-c C-c from a buffer without a shebang still shows `Python 2.7.6` as the interpreter being used.

Revision history for this message
DougRenze (drenze) said :
#8

Scratch part of that. The (custom-set-variables...) did NOT work in init.el.

Revision history for this message
Andreas Roehler (a-roehler) said :
#9

On 25.06.2014 00:22, DougRenze wrote:
> Question #250443 on python-mode.el changed:
> https://answers.launchpad.net/python-mode/+question/250443
>
> DougRenze posted a new comment:
> Scratch part of that. The (custom-set-variables...) did NOT work in
> init.el.
>

Hi Doug,

the cooperation between defcustom-set variables and setting otherwise was a hairy issue ever.
The only clean way IMHO is to make a choice how to set defaults.

Beside defaults, python-mode is calculating the appropriate shell at numerous positions, thus py-shell-name might be re-set.

Please a file bug report when the wrong shell --in real context-- is picked.

Cheers,

Andreas

Revision history for this message
DougRenze (drenze) said :
#10

It is consistently...that's the bug report I've already filed.

Thanks.