GNU bug report logs - #13570
24.2; (python.el) "ValueError: Attempted relative import in non-package" with relative import.

Previous Next

Package: emacs;

Reported by: Oleksandr Gavenko <gavenkoa <at> gmail.com>

Date: Sun, 27 Jan 2013 22:41:01 UTC

Severity: normal

Fixed in version 24.2

Done: fgallina <at> gnu.org (Fabián Ezequiel Gallina)

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 13570 in the body.
You can then email your comments to 13570 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#13570; Package emacs. (Sun, 27 Jan 2013 22:41:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to Oleksandr Gavenko <gavenkoa <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Sun, 27 Jan 2013 22:41:01 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Oleksandr Gavenko <gavenkoa <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: 24.2;
	(python.el) "ValueError: Attempted relative import in non-package"
	with relative import.
Date: Mon, 28 Jan 2013 00:40:20 +0200
In GNU Emacs 24.2.1 (x86_64-pc-linux-gnu, GTK+ Version 3.4.2)
 of 2013-01-21 on trouble, modified by Debian

  # mkdir package
  # cd package

  # cat <<EOF >cmd.py
  from . import lib
  lib.ok()
  EOF

  # cat <<EOF >lib.py
  def ok():
      print('ok')
  EOF

When you open cmd.py and try C-c C-l (python-load-file) you get:

  >>> Traceback (most recent call last):
  Traceback (most recent call last):
    File "/home/user/devel/tmp/package/cmd.py", line 1, in <module>
      from . import lib
  ValueError: Attempted relative import in non-package

With C-c C-c (python-send-string) ever worse: it uses (make-temp-file "py") so
relative import is not possible!!

-- 
Best regards!




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13570; Package emacs. (Mon, 28 Jan 2013 21:10:02 GMT) Full text and rfc822 format available.

Message #8 received at 13570 <at> debbugs.gnu.org (full text, mbox):

From: Oleksandr Gavenko <gavenkoa <at> gmail.com>
To: 13570 <at> debbugs.gnu.org
Subject: [PATCH] 24.2;
	(python.el) "ValueError: Attempted relative import in non-package"
	with relative import.
Date: Mon, 28 Jan 2013 23:08:48 +0200
As I previously wrote python.el on C-c C-l (python-load-file) throw:

  ValueError: Attempted relative import in non-package

if module have relative import, like:

  from . import module
  from ..sound import module

Seems that 'eimport' from emacs2.py/emacs3.py already support packages. So I
improve 'python-load-file' in python.el by locating most top non-package
directory, see example:

  $ mkdir /home/user/pythonpath/package1/package2/
  $ touch /home/user/pythonpath/package1/package2/__init__.py
  $ touch /home/user/pythonpath/package1/__init__.py

  (defun foo ()
    (let ( (module "module") (path "/home/user/pythonpath/package1/package2/") )
      (while (file-exists-p (concat path "__init__.py"))
        (string-match "\\(.+/\\)\\([^/]+\\)/$" path)
        (setq module (concat (match-string 2 path) "." module))
        (setq path (match-string 1 path))
        )
      (list path module)))

The patch is:

--- old/python.el	2013-01-28 22:56:50.000000000 +0200
+++ new/python.el	2013-01-28 22:59:24.000000000 +0200
@@ -1703,8 +1703,12 @@
     (python-send-command
      (if (string-match "\\.py\\'" file-name)
 	 (let ((module (file-name-sans-extension
-			(file-name-nondirectory file-name))))
-	   (format "emacs.eimport(%S,%S)"
-		   module (file-name-directory file-name)))
+                        (file-name-nondirectory file-name)))
+               (path (file-name-directory file-name)))
+           (while (file-exists-p (concat path "__init__.py"))
+             (string-match "\\(.+/\\)\\([^/]+\\)/$" path)
+             (setq module (concat (match-string 2 path) "." module))
+             (setq path (match-string 1 path)))
+	   (format "emacs.eimport(%S,%S)" module path))
        (format "execfile(%S)" file-name)))
     (message "%s loaded" file-name)))


But newer python.el version no longer use emacs2.el.

I have no time to compile and debug latest Emacs but suggest use similar
technique to allow relative import...

-- 
Best regards!




Reply sent to fgallina <at> gnu.org (Fabián Ezequiel Gallina):
You have taken responsibility. (Sun, 27 Jul 2014 06:42:02 GMT) Full text and rfc822 format available.

Notification sent to Oleksandr Gavenko <gavenkoa <at> gmail.com>:
bug acknowledged by developer. (Sun, 27 Jul 2014 06:42:03 GMT) Full text and rfc822 format available.

Message #13 received at 13570-done <at> debbugs.gnu.org (full text, mbox):

From: fgallina <at> gnu.org (Fabián Ezequiel Gallina)
To: 13570-done <at> debbugs.gnu.org
Subject: 24.2;
 (python.el) "ValueError: Attempted relative import in non-package"
 with relative import.
Date: Sun, 27 Jul 2014 03:41:52 -0300
Fixed at revno 117584 in trunk.

The new `python-shell-package-enable' command let's you enable a package
for the current shell and use relative imports in evaled code.


Cheers,
Fabián




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sun, 24 Aug 2014 11:24:04 GMT) Full text and rfc822 format available.

This bug report was last modified 9 years and 273 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.