relative import broken?

T

test

basic noob question here.

i am trying to reference a package, i have the structure:

mypack/
__init__.py
test.py
subdir1/
__init__.py
mod1.py
subdir2/
__init__.py
mod2.py

can someone please tell me why the statement:

from mypack.subdir1.mod1 import *

does NOT work from mod2.py nor from test.py?

instead, if i use:

from subdir1.mod1 import *

it works perfectly from test.py.

.....?

thank you,

aj.
 
P

Peter Otten

test said:
basic noob question here.

i am trying to reference a package, i have the structure:

mypack/
__init__.py
test.py
subdir1/
__init__.py
mod1.py
subdir2/
__init__.py
mod2.py

can someone please tell me why the statement:

from mypack.subdir1.mod1 import *

does NOT work from mod2.py nor from test.py?

instead, if i use:

from subdir1.mod1 import *

it works perfectly from test.py.

....?

The parent directory of mypack must be in the module search path, see

http://docs.python.org/tut/node8.html#l2h-19

The least intrusive way to achieve this is to move test.py one level up in
the directory hierarchy.

Peter
 
S

Sam

I also have a problem with relative import; I can't for the life of me
figure out how to use the damn thing. I think the main problem is with
getting Python to recognize the existence of a package. I have

S/
p.py
B/
b.py
W/
pyw/
u.py
ws.py

and I'd like to get u.py to import all the other 3 programs. I put
empty __init__.py files in all of the above directories (is this
necessary?), and even manually added the pathway (r'C:\Myname\S') to
sys.path, but when I execute

from S import p

in u.py Python gives "ImportError: No module named S". It says "No
module named X" for essentially any package reference, so I think it's
just not recognizing the directories as packages. The docs for
relative import make this sound much easier than it is.

Thanks in advance, I'm at my wit's end.

-Sam
 
H

Hrvoje Niksic

Sam said:
I also have a problem with relative import; I can't for the life of me
figure out how to use the damn thing. I think the main problem is with
getting Python to recognize the existence of a package. I have

S/
p.py
B/
b.py
W/
pyw/
u.py
ws.py

and I'd like to get u.py to import all the other 3 programs. I put
empty __init__.py files in all of the above directories (is this
necessary?), and even manually added the pathway (r'C:\Myname\S') to
sys.path, but when I execute

from S import p

in u.py Python gives "ImportError: No module named S".

A silly question: is the directory that contains "S" in PYTHONPATH or
in sys.path?
The docs for relative import make this sound much easier than it is.

It's supposed to be just as easy as it sounds. For example:

$ mkdir S
$ touch S/p.py
$ touch S/__init__.py
$ python
Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.<module 'S.p' from 'S/p.py'>
 
S

Sam

A silly question: is the directory that contains "S" in PYTHONPATH or
in sys.path?

It's in sys.path. I'm not sure how to access or change PYTHONPATH from
within a program (I'm running everything in IDLE). I'm using Windows,
btw.
The docs for relative import make this sound much easier than it is.

It's supposed to be just as easy as it sounds. For example:

$ mkdir S
$ touch S/p.py
$ touch S/__init__.py
$ python
Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.>>> from S import p
<module 'S.p' from 'S/p.py'>
 
S

Sam

A silly question: is the directory that contains "S" in PYTHONPATH or
in sys.path?

It's in sys.path. I'm not sure how to access or change PYTHONPATH from
within a program (I'm running everything in IDLE). I'm using Windows,
btw.
The docs for relative import make this sound much easier than it is.

It's supposed to be just as easy as it sounds. For example:

$ mkdir S
$ touch S/p.py
$ touch S/__init__.py
$ python
Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.>>> from S import p
<module 'S.p' from 'S/p.py'>
 
S

Sam

I didn't look it up myself, but maybe a __init__.py file is missing
so that it can be recognised as a package.

No, every single one (S, B, and W) has its own __init__.py
"S" or its parent directory?

I added r'C:\Users\Myname\S' to sys.path. Is this the correct usage?

Sam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top