问题
I'm new to Emacs and I'm trying to set up my python environment. So far I've learned that using "python-mode.el" in a python buffer C-c C-c
loads the contents of the current buffer into an interactive python shell, apparently using what which python
yields. In my case that is python 3.3.3. But since I need to get a python 2.7 shell, I'm trying to get Emacs to spawn such a shell on C-c C-c
. Unfortunatly I can't figure out, how to do this. Setting py-shell-name
to what which python2.7
yields (i.e. /usr/bin/python2.7
) does not work. How can get Emacs to do this, or how can I trace back what Emacs executes when I hit C-c C-c
?
回答1:
python-mode.el, execute a python buffer using python2:
M-x py-execute-buffer-python2
or put this in .emacs file:
(custom-set-variables
'(py-force-py-shell-name-p t)
'(py-shell-name "python2"))
python-mode.el checks py-force-py-shell-name-p
variable when executing py-execute-buffer
(bound to C-c C-c
key), and if this variable is set to true("t"), then use python interpreter name saved in py-shell-name
.
Alternatively, this customization can be done in M-x customize
, Programming>Languages>Python Mode, search there for "Py Force Py Shell" and "Py Shell Name" lines.
It will add this customization code to your .emacs file.
Emacs help(describe function):
C-h f py-execute-buffer TAB
You can send selected region in a python buffer to any interpreter:
C-u 3 M-x py-execute-region
Emacs will prompt every time for a python interpreter name you want to use. The prefix numerical argument may be any number except 1 or 4, otherwise it will use a default interpreter without prompt.
To execute a buffer in different python interpreters you can select whole buffer by C-x h
and then use this prefixed command.
回答2:
I don't use python, but from the source to python-mode, I think you should look into customizing the variable python-python-command
- It seems to default to the first path command matching "python"; perhaps you can supply it with a custom path?
来源:https://stackoverflow.com/questions/21588464/using-python2-7-with-emacs-24-3-and-python-mode-el