unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#13570: 24.2; (python.el) "ValueError: Attempted relative import in non-package" with relative import.
@ 2013-01-27 22:40 Oleksandr Gavenko
  2013-01-28 21:08 ` bug#13570: [PATCH] " Oleksandr Gavenko
  2014-07-27  6:41 ` bug#13570: " Fabián Ezequiel Gallina
  0 siblings, 2 replies; 3+ messages in thread
From: Oleksandr Gavenko @ 2013-01-27 22:40 UTC (permalink / raw)
  To: 13570

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!





^ permalink raw reply	[flat|nested] 3+ messages in thread

* bug#13570: [PATCH] 24.2; (python.el) "ValueError: Attempted relative import in non-package" with relative import.
  2013-01-27 22:40 bug#13570: 24.2; (python.el) "ValueError: Attempted relative import in non-package" with relative import Oleksandr Gavenko
@ 2013-01-28 21:08 ` Oleksandr Gavenko
  2014-07-27  6:41 ` bug#13570: " Fabián Ezequiel Gallina
  1 sibling, 0 replies; 3+ messages in thread
From: Oleksandr Gavenko @ 2013-01-28 21:08 UTC (permalink / raw)
  To: 13570

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!





^ permalink raw reply	[flat|nested] 3+ messages in thread

* bug#13570: 24.2; (python.el) "ValueError: Attempted relative import in non-package" with relative import.
  2013-01-27 22:40 bug#13570: 24.2; (python.el) "ValueError: Attempted relative import in non-package" with relative import Oleksandr Gavenko
  2013-01-28 21:08 ` bug#13570: [PATCH] " Oleksandr Gavenko
@ 2014-07-27  6:41 ` Fabián Ezequiel Gallina
  1 sibling, 0 replies; 3+ messages in thread
From: Fabián Ezequiel Gallina @ 2014-07-27  6:41 UTC (permalink / raw)
  To: 13570-done


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





^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-07-27  6:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-27 22:40 bug#13570: 24.2; (python.el) "ValueError: Attempted relative import in non-package" with relative import Oleksandr Gavenko
2013-01-28 21:08 ` bug#13570: [PATCH] " Oleksandr Gavenko
2014-07-27  6:41 ` bug#13570: " Fabián Ezequiel Gallina

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).