You are not logged in.

#1 2012-05-28 11:18:02

tbdc
Member
Registered: 2012-04-25
Posts: 22

Emacs and Python 3

I have only just starting learning how to use emacs and have been trying to get it to work as a python3 ide. I have followed a couple guides I have found - none which though have explicitly mentioning python3 - namely http://www.emacswiki.org/emacs/?action= … ingInEmacs and http://pedrokroger.net/2010/07/configur … hon-ide-2/. I have found a few (dodgy) workarounds to some of the issues I've had and wanted to share them as well as ask if anyone else had encountered these problems and had any better suggestions.

I am using ipython, python-mode, pymacs and anything.

Here is my .emacs

(add-to-list 'load-path "~/elisp")
(require 'python-mode)
(setq py-python-command "ipython")
;; (defadvice py-execute-buffer (around python-keep-focus activate)
;;   "Thie advice to make focus python source code after execute command `py-execute-buffer'."
;;   (let ((remember-window (selected-window))
;;         (remember-point (point)))
;;     ad-do-it
;;     (select-window remember-window)
;;     (goto-char remember-point)))

;; (defun rgr/python-execute()
;;   (interactive)
;;   (if mark-active
;;       (py-execute-string (buffer-substring-no-properties (region-beginning) (region-end)))
;;     (py-execute-buffer)))

;; (global-set-key (kbd "C-c C-e") 'rgr/python-execute)

 (require 'anything-ipython)
 (add-hook 'python-mode-hook #'(lambda ()
                                 (define-key py-mode-map (kbd "M-<tab>") 'anything-ipython-complete)))
 (add-hook 'ipython-shell-hook #'(lambda ()
                                   (define-key py-mode-map (kbd "M-<tab>") 'anything-ipython-complete)))

(when (require 'anything-show-completion nil t)
   (use-anything-show-completion 'anything-ipython-complete
                                 '(length initial-pattern)))


(custom-set-variables
  ;; custom-set-variables was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(py-python-command-args (quote ("--colors=LightBG" "-i" "--profile=emacs"))))
(custom-set-faces
  ;; custom-set-faces was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 )

(autoload 'pymacs-apply "pymacs")
(autoload 'pymacs-call "pymacs")
(autoload 'pymacs-eval "pymacs" nil t)
(autoload 'pymacs-exec "pymacs" nil t)
(autoload 'pymacs-load "pymacs" nil t)
(autoload 'pymacs-autoload "pymacs")
;;(eval-after-load "pymacs"
;;  '(add-to-list 'pymacs-load-path YOUR-PYMACS-DIRECTORY"))

The first problem I had was that C-c C-c, would not work, the ipython buffer would complain that execfile() doesn't exist - which in python3 it doesn't. To get round this I've created an 'emacs' ipython profile that runs this script at startup:

def execfile(file, globals=globals(), locals=locals()):
    with open(file, 'r') as f:
        exec(f.read()+'\n', globals, locals)

Although this now allows me successfully execute code from the buffer, its a bit of a 'hackish' workaround. Is python3 not officiallly supported by python-mode? Is there a neater solution?

The other problem I had is that pymacs, which says it now supports python3, wouldn't make and install if the python interpretor was set to python3. The problem was that *.py files used in the installation of pymacs were opening UTF-8 files with default encoding - being ascii in python3. After changing all all the

open('somefile')

statements in the *.py files to

open('somefile', ecoding='UTF-8')

pymacs finally installed. I tested the installation of pymacs as suggested in the pymacs guide in python shell by trying 'from Pymacs import lisp'. At first this worked fine in python2 and 3 then suddenly stopped working - but only in python3. Python would complain of invalid syntax in the line

except ProtocolError, exception:

in Pymacs.py. After changing this line to

except ProtocolError as exception:

pymacs will import fine. Is this a problem with pymacs or something I have done?

The things that I have not managed to get working at all are:
Code completion using anything as described in http://pedrokroger.net/2010/07/configur … hon-ide-2/, M-tab completes but does not give options or show the anything buffer and will often simply auto complete to 'False'.

M-x pymacs-eval returns an error - Pymacs helper did not start within 30 seconds.

Any suggestions on these issues?
Has anybody managed a successful python3 environment in emacs?

Last edited by tbdc (2012-05-30 13:59:42)

Offline

#2 2012-05-28 15:42:45

tbdc
Member
Registered: 2012-04-25
Posts: 22

Re: Emacs and Python 3

After trying many things to get pymacs working and not succeeding,  I tried installing python3, pymacs and emacs on a live cd of ubuntu LTS. Pymacs works fine, no errors, no messing around needed during installation. What could be going wrong with my attempts on arch!?

Offline

#3 2012-05-28 16:27:05

tbdc
Member
Registered: 2012-04-25
Posts: 22

Re: Emacs and Python 3

Ok....I removed all of .el files and downloaded a fresh copy pymacs. On 'make install' it complains about the encoding issue - as previously mentioned. When exactly the same is carried out in ubuntu no errors occurred. After modifying all of the .py files that come with pymacs so that the encoding is set to UTF-8. 'make install' then works. Pymacs successfully loads as module in python3. I deleted the contents of my .emacs file and replaced it with simply:
(add-to-list 'load-path "~/elisp/pinard-Pymacs-5989046")
(require 'pymacs)
When I do, M-x pymacs-eval print('Hello') in emacs I get the following errors:
Pymacs helper did not start within 30 seconds, in the minibufffer and

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.2/site-packages/Pymacs.py", line 127, in main
    lisp._protocol.send('version', '"%s"' % version)
  File "/usr/lib/python3.2/site-packages/Pymacs.py", line 392, in send
    sys.stdout.write(prefix + text)
TypeError: 'str' does not support the buffer interface

Process pymacs exited abnormally with code 1

in the pymacs buffer.

What is going wrong!?

Offline

#4 2012-06-12 05:48:44

tnyplz
Member
From: North Hills, CA
Registered: 2012-06-12
Posts: 1
Website

Re: Emacs and Python 3

I was able to fix the execfile error by adding the following line in my .emacs after loading python-mode:

(setq py-shell-name "/usr/bin/python3")

Offline

#5 2012-06-12 09:53:01

tbdc
Member
Registered: 2012-04-25
Posts: 22

Re: Emacs and Python 3

I've just tried putting this in my .emacs and it complains that "process /usr/bin/python3 is not running". I usually have this variable set to ipython, and have (py-python-command-args (quote ("-i" "--profile=emacs"))), which loads a ipython profile which on autostart defines the execfile function. It would be nice to get your solution working as it is neater, though I do like the advantages of ipython shell over a normal python3 one.
Also I have managed to get pymacs working, there appears to be a bug with the latest version and python 3 however the 0.24-betav2 version works just fine, you can get it from here

Last edited by tbdc (2012-06-12 09:53:18)

Offline

#6 2016-05-28 05:46:09

jethronsun7
Member
Registered: 2016-04-29
Posts: 44
Website

Re: Emacs and Python 3

I know it is an old thread but I thought somehow someone else may want to take another look.

For Emacs I recommend Spacemacs and purcell's emacs.d. Start to use some outofbox configured Emacs will always be a good one.

Offline

#7 2016-05-28 11:18:30

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 11,858
Website

Re: Emacs and Python 3


Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

Offline

Board footer

Powered by FluxBB