2

I'm getting a really weird problem in my long experience with python..

In advance i want to say that i want to know WHY does this happen, and not how to change my code or how to fix it because i can do it too.

I'm using python2.7.3 and using the __future__ absolute_import feature. I have a little complex module structure.. I have a module q.x.y.z. If at the top I do import q.x.a.b it works, but if i do import q.x.a.b as _b it fails saying that q.x doesn't have "a" (which have been imported a couple of times before this module) anyone has enough experience to know what's happening? I'm pretty sure its not a circular import problem because I only need to remove the "as _b" and it magically works. And also is not a naming problem because I'm really careful about that.

file structure is as follows:

q/
+ __init__.py
+ main.py #execution entry point
--- x/
    + __init__
    + a/
      + __init__
      + b.py 
   + y/
      + __init__
      + z.py # import q.x.a.b

its ran from the parent folder of q pkg> python q\main.py and it contains this:

from __future__ import absolute_import
import sys, os

if __name__=='__main__':
    sys.path[0] = os.getcwd()
    import q
    q.run()

a little related to circular imports but yet the main question is WHY does it happens when i use "as"?

the traceback goes something like this:

q/main.py: q.run()
q/x/__init__.py : from q.x import a, y, k
q/x/a/__init__.py : from q.x.a import d, e, f
q/x/a/f.py : from q.x.y import z as _z
q/x/y/__init__.py : from q.x.y import g, h, z
q/x/y/h.py : import q.x.a.d as _d

could this has something to do? https://stackoverflow.com/a/1835089/260242 or this https://stackoverflow.com/a/11309252/260242

a sample of the code, which actually fails... go to test/q/x/y/h.py and change import ... as _d to import ... and you'll see http://puu.sh/16C3j

Community
  • 1
  • 1
Nande
  • 409
  • 1
  • 6
  • 11
  • How do you execute your code? I mean, what is the content of "main" and which is your working directory when you launch it? – Bakuriu Sep 19 '12 at 12:43
  • 1
    @Nande -- It helps if you `edit` your question and add that information there. That makes it a lot easier for us to parse, etc. – mgilson Sep 19 '12 at 12:55
  • Regarding `sys.path[0] = os.getcwd()`: are you positive you want to overwrite the first element of `sys.path` and not prepend your cwd, as in `sys.path[0:0] = [os.getcwd()]`? – 9000 Sep 19 '12 at 15:44
  • yes. and how that apply to the question? – Nande Sep 19 '12 at 16:10

1 Answers1

2

Sorry, cannot reproduce, at least using Python 2.6.5 I have around.

Please take time to isolate a minimal case that exhibits the problem in a clean tree. I'd start with a copy of your current tree, pruning any unrelated directories and deleting function definitions and such, all this constantly checking that the problem is still present. The process would either converge to a minimal case showing a bug, or (more probably) show a seemingly unrelated thing that really causes this strange behavior.

Here's my work log from a Linux console; you can replay it an an empty directory.

mkdir -p q/{x,y}
echo > q/__init__.py
echo > q/x/__init__.py
echo > q/y/__init__.py
mkdir -p q/x/a
echo > q/x/a/__init__.py
echo "place='This is q.x.a.b'" > q/x/a/b.py
echo -e "import q.x.a.b as _b\n\nprint _b.place" > q/y/z.py
python -c 'from __future__ import absolute_import; import q.y.z'

Expectedly it prints This is q.x.a.b.

9000
  • 39,899
  • 9
  • 66
  • 104
  • i'm sorry buddy but read the question again, is a little more complicated than that. i'll leave you a sample there. – Nande Sep 19 '12 at 14:04