python 3 in emacs

14,243

Solution 1

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.

Solution 2

  • [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.

Solution 3

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.

Solution 4

start an python-interpreter

M-x python RET

(the default interpreter)

M-x pythonVERSION

where VERSION means any installed version

Solution 5

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.

Share:
14,243
YumTum
Author by

YumTum

Let's get the tank loaded up!

Updated on June 04, 2022

Comments

  • YumTum
    YumTum almost 2 years

    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!

  • YumTum
    YumTum almost 12 years
    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?
  • Greg E.
    Greg E. almost 12 years
    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"))).
  • Greg E.
    Greg E. almost 12 years
    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.