unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Oleksandr Gavenko <gavenkoa@gmail.com>
To: 13570@debbugs.gnu.org
Subject: bug#13570: [PATCH] 24.2; (python.el) "ValueError: Attempted relative import in non-package" with relative import.
Date: Mon, 28 Jan 2013 23:08:48 +0200	[thread overview]
Message-ID: <87txq1ypzz.fsf@gavenkoa.example.com> (raw)
In-Reply-To: <87a9rup7vv.fsf@gavenkoa.example.com>

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	other threads:[~2013-01-28 21:08 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2014-07-27  6:41 ` Fabián Ezequiel Gallina

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87txq1ypzz.fsf@gavenkoa.example.com \
    --to=gavenkoa@gmail.com \
    --cc=13570@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).