Both Python 2 and 3 in Emacs

后端 未结 5 995
不知归路
不知归路 2020-12-14 02:56

I have been using Emacs to write Python 2 code. Now I have both Python 2.6 and 3.0 installed on my system, and I need to write Python 3 code as well.

Here is how the

5条回答
  •  萌比男神i
    2020-12-14 03:26

    My comment on this answer.

    I wrote /t/min.py which will run fine in python3 but not in python2 (dictionary comprehension works in python3)

    Contents of /t/min.py

    #!/usr/bin/python3
    # -*- py-python-command: "/usr/bin/python3"; -*-
    a = {i:i**2 for i in range(10)}
    print(a)
    

    Note that the shebang indicates python3 and the file local variable py-python-command too.

    I also wrote /t/min-py.el which makes sure that python-mode.el (ver 5.1.0)is used instead of python.el.

    Contents of /t/min-py.el

    (add-to-list 'load-path "~/m/em/lisp/")
    (autoload 'python-mode "python-mode" "Python Mode." t)
    ;; (setq py-python-command "python3")
    

    Note that the last line is commented out.

    I start emacs with the following command:

    emacs -Q -l /t/min-py.el /t/min.py &
    

    Now emacs is started with my alternate dotemacs /t/min-py.el and it opens /t/min.py.

    When I press C-c C-c to send the buffer to python, it says the "for" part is wrong and that indicates that python2 is being used instead of python3. When I press C-c ! to start the python interpreter, it says python 2.5 is started.

    I can even change the second line of /t/min.py to this:

    # -*- py-python-command: "chunkybacon"; -*-
    

    and do this experiment again and emacs still uses python2.

    If the last line of /t/min-py.el is not commented out, then C-c C-c and C-c ! both use python3.

提交回复
热议问题