changing sys.path

Dave Angel d at davea.name
Fri Feb 10 10:06:28 EST 2012


On 02/10/2012 09:51 AM, Andrea Crotti wrote:
> Ok now it's getting really confusing, I tried a small example to see
> what is the real behaviour,
> so I created some package namespaces (where the __init__.py declare the
> namespace package).
>
> /home/andrea/test_ns:
> total used in directory 12 available 5655372
> drwxr-xr-x 3 andrea andrea 4096 Feb 10 14:46 a.b
> drwxr-xr-x 3 andrea andrea 4096 Feb 10 14:46 a.c
> -rw-r--r-- 1 andrea andrea 125 Feb 10 14:46 test.py
>
> /home/andrea/test_ns/a.b:
> total 8
> drwxr-xr-x 3 andrea andrea 4096 Feb 10 14:47 a
> -rw-r--r-- 1 andrea andrea 56 Feb 10 14:35 __init__.py
>
> /home/andrea/test_ns/a.b/a:
> total 8
> drwxr-xr-x 2 andrea andrea 4096 Feb 10 14:47 b
> -rw-r--r-- 1 andrea andrea 56 Feb 10 14:35 __init__.py
>
> /home/andrea/test_ns/a.b/a/b:
> total 12
> -rw-r--r-- 1 andrea andrea 25 Feb 10 14:36 api.py
> -rw-r--r-- 1 andrea andrea 153 Feb 10 14:37 api.pyc
> -rw-r--r-- 1 andrea andrea 56 Feb 10 14:35 __init__.py
>
> /home/andrea/test_ns/a.c:
> total 8
> drwxr-xr-x 3 andrea andrea 4096 Feb 10 14:47 a
> -rw-r--r-- 1 andrea andrea 56 Feb 10 14:35 __init__.py
>
> /home/andrea/test_ns/a.c/a:
> total 8
> drwxr-xr-x 2 andrea andrea 4096 Feb 10 14:47 c
> -rw-r--r-- 1 andrea andrea 56 Feb 10 14:35 __init__.py
>
> /home/andrea/test_ns/a.c/a/c:
> total 12
> -rw-r--r-- 1 andrea andrea 20 Feb 10 14:36 api.py
> -rw-r--r-- 1 andrea andrea 148 Feb 10 14:38 api.pyc
> -rw-r--r-- 1 andrea andrea 56 Feb 10 14:35 __init__.py
>
>
> So this test.py works perfectly:
> import sys
> sys.path.insert(0, 'a.c')
> sys.path.insert(0, 'a.b')
>
> from a.b import api as api_ab
>
> from a.c import api as api_ac
>
> While just mixing the order:
> import sys
> sys.path.insert(0, 'a.b')
>
> from a.b import api as api_ab
>
> sys.path.insert(0, 'a.c')
> from a.c import api as api_ac
>
> Doesn't work anymore
>
> [andrea at precision test_ns]$ python2 test.py
> Traceback (most recent call last):
> File "test.py", line 7, in <module>
> from a.c import api as api_ac
> ImportError: No module named c
>
>
>
> Am I missing something/doing something stupid?

Yes, you've got periods in your directory names.  A period means 
something special within python, and specifically within the import.

When you say  from a.c import api

You're telling it:    from package a  get module c, and from there 
impoort the symbol api

But package a has no module c, so it complains.


In an earlier message you asserted you were using all absolute paths in 
your additions to sys.path.  Here you're inserting relative ones.  How come?

-- 

DaveA



More information about the Python-list mailing list