Reputation: 389
I changed two days ago to Emacs 23, which lately gave me a lot of headache, especially, as I have two Python versions installed, the older 2.7 and 3. As I generally want to start the python 3 interpreter, it would be nice if I could tell Emacs in some way to use python 3 instead of 2.7.
Besides, I could not find a module which helps to highlight python3 syntax. I am currently using python-mode.el for highlighting.
Also, if somebody had a good tip for which module would be best to show the pydoc, I would be very thankful.
Thanks in advance!
Upvotes: 17
Views: 15276
Reputation: 43
It's December 2020, I'm using EMACS 25, otherwise unconfigured for python, and I found that:
(setq python-shell-interpreter "python3")
added to my .emacs file did the business and led to a reasonably intuitive way of working with python3. C-c C-c
sends my file to the interpreter (or asks me to start a new interpreter, and tells me how).
Of course, now the python3 interpreter runs for any python file, so python 2 programs don't work.
I am imagining a better world where emacs looks at the #!/usr/bin/env python2
line at the top of my file and figures out which interpreter to start by some kind of insane high-tech magic.
Upvotes: 1
Reputation: 38267
If you feel like letting Emacs do the heavy lifting for you, go through the Emacs settings dialogue to have Emacs set it automatically.
M-x customize-variable [RET] python-shell-interpreter [RET]
A new buffer should appear where you can customize the python interpreter you are using.
The field to the right of Python Shell Interpreter:
will be the current interpreter your are using. In my case it was python
so I changed it to python3
. Then move the curer to select the Apply and Save
button above and hit [RET] to make Emacs respect the new setting. Alternatively you can click the button with the mouse.
Next time you open the Emacs python interpreter, it will be using the new python you set from the setting dialogue.
Upvotes: 10
Reputation: 10350
Inspired by @serv-inc's answer you can also put
(setq python-shell-interpreter "python3")
into your ./.emacs.d/init.el
or where you store your configuration.
Upvotes: 1
Reputation: 41
After I did the following thing, I got the behavior that: when editing a Python file in emacs, C-c C-p creates a new buffer with a Python3 interpreter.
Add to your .emacs file the following:
(defcustom python-shell-interpreter "python3"
"Default Python interpreter for shell."
:type 'string
:group 'python)
The reason I tried this, was because I found
(defcustom python-shell-interpreter "python"
"Default Python interpreter for shell."
:type 'string
:group 'python)
in python.el. And I hypothesized (i) that the "python" could be substituted with "python3" and (ii) that this would override the definition in python.el.
It is likely that there are reasons why this is inelegant or heavy-handed or otherwise bad. I am an emacs newb.
Upvotes: 4
Reputation: 4804
start an python-interpreter
M-x python RET
(the default interpreter)
M-x pythonVERSION
where VERSION means any installed version
Upvotes: 1
Reputation: 4804
Note python-mode.el knows a hierarchy how to detect the version needed
a shebang precedes setting of py-shell-name while py-execute-THING-PYTHONVERSION
would precede also shebang for the command being
see menu PyExec
Upvotes: 0
Reputation: 2742
If you're using python-mode.el
, you can specify the binary to be executed as an inferior process by setting the py-python-command
variable, i.e.:
(setq py-python-command "python3")
Naturally, you'll need to provide the name of the binary as it exists on your system in place of "python3"
, if it differs. In python.el
, the analogous variable to set is python-python-command
.
As far as using pydoc
, there are a few possibilities. First, you can simply execute help()
inside the Python inferior process. If you do choose that option, you might find it useful to add the following code to your .emacs
file:
(setenv "PAGER" "cat")
This is necessary because interactive pagers (e.g., less
, more
, most
, etc.) don't work particularly well inside inferior process buffers. Second, you can install a Texinfo package containing the documentation and use Emacs's info
browser (q.v., Python Programming in Emacs). Finally, if you opt to use python.el
, it includes an interactive function called python-describe-symbol
that can lookup pydoc
help on demand (and I suspect python-mode.el
should have something similar). If you search around a bit, I'm sure you can find other methods and/or third-party packages as well.
Upvotes: 13