python 3 in emacs
Asked Answered
R

7

17

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!

Ramiform answered 17/6, 2012 at 13:38 Comment(0)
W
13

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.

Waylan answered 17/6, 2012 at 14:31 Comment(3)
Thanks so far for your answer! Yet if I put (setq py-python-command "/usr/bin/python3") in .emacs, i does not seem to make the job. I double checked the path to python, it seems to be correct. I also tried it with python-python-command, in the case python-mode.el just does not work, but again, no result. Any ideas?Ramiform
If the python3 binary is in your system $PATH, you shouldn't need to specify the full, explicit directory path. Try putting that command inside the python-mode-hook in your .emacs file, i.e.: (add-hook 'python-mode-hook #'(lambda () (setq py-python-command "python3"))).Waylan
At present, I'm using python.el, so there may be something I'm forgetting w/r/t to python-mode.el. You may also need to set the py-which-shell variable in a similar fashion as well, though I don't recall having to do that when I worked with python-mode.el.Waylan
U
10
  • [RET] = enter or return key.
  • M = Meta or Alt key.

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.

Unglue answered 3/6, 2019 at 10:56 Comment(0)
D
4

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.

Daley answered 1/3, 2017 at 17:5 Comment(0)
P
1

start an python-interpreter

M-x python RET

(the default interpreter)

M-x pythonVERSION

where VERSION means any installed version

Pull answered 23/7, 2012 at 17:45 Comment(0)
S
1

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.

Schizogony answered 15/10, 2019 at 11:55 Comment(0)
T
1

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.

Theone answered 15/12, 2020 at 13:19 Comment(0)
P
0

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

Pull answered 23/7, 2012 at 11:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.