package questions

D

Daniel

Hello,

I've posted about this before, but after reading the docs, I have a
few more questions
here are the docs: http://docs.python.org/tutorial/modules.html#packages
here is my previous post:
http://groups.google.com/group/comp...read/thread/3a352159f6828eb9/cda8395d36827d20

I've setup some directories for the sound package, exactly as shown in
the examples in the docs. In the surround file I put the lines

from . import echo
from .. import formats
from ..filters import equalizer

again, exactly as in the docs. When I run surround.py, I get the
following result:
C:\sound\effects>python surround.py
Traceback (most recent call last):
File "surround.py", line 1, in <module>
from . import echo
ValueError: Attempted relative import in non-package

C:\sound\effects>

Why doesn't this work as suggested in the docs. The only way that I
found to get around this is to add directories to the path, but I
can't help but think that it shouldn't be necessary to modify the path
for packages to work.

Just to be sure, here are directory listings showing the I have the
__init__.py files in place

C:\sound\effects>dir
Volume in drive C is Default
Volume Serial Number is 8CD7-71F6

Directory of C:\sound\effects

05/22/2009 10:07 AM <DIR> .
05/22/2009 10:07 AM <DIR> ..
05/22/2009 10:06 AM 0 echo.py
05/22/2009 10:16 AM 189 surround.py
05/22/2009 10:04 AM 0 __init__.py
3 File(s) 189 bytes
2 Dir(s) 50,860,060,672 bytes free

C:\POP\sound\effects>dir ..\
Volume in drive C is Default
Volume Serial Number is 8CD7-71F6

Directory of C:\sound

05/22/2009 10:04 AM <DIR> .
05/22/2009 10:04 AM <DIR> ..
05/22/2009 10:07 AM <DIR> effects
05/22/2009 10:06 AM <DIR> filters
05/22/2009 10:04 AM <DIR> formats
05/22/2009 10:04 AM 0 __init__.py
1 File(s) 0 bytes
5 Dir(s) 50,859,077,632 bytes free

C:\sound\effects>dir ..\filters
Volume in drive C is Default
Volume Serial Number is 8CD7-71F6

Directory of C:\sound\filters

05/22/2009 10:06 AM <DIR> .
05/22/2009 10:06 AM <DIR> ..
05/22/2009 10:06 AM 0 equalizer.py
05/22/2009 10:04 AM 0 __init__.py
2 File(s) 0 bytes
2 Dir(s) 50,859,077,632 bytes free

Please share any feedback. Thanks.
 
D

Daniel

(1) You really should identify your OS and Python version.  I am fairly
certain it is some form of Windows, but ....
Try something like:
     C:\sound\effects> cd C:\
     C:\> python -m sound.effects.surround

(2) you can run
     python -v surround.py
or
     python -vv surround.py
To see files opened (or attempted and opened for -vv) in order to
discover what exactly is tried and in what order on imports.
Warning: the output is huge.

--Scott David Daniels
(e-mail address removed)

I'm on WinXP using Python 2.5 Sorry I didn't mention that at first.

C:\sound\effects>cd ..\..\

C:\>python -m sound.effects.surround
Traceback (most recent call last):
File "C:\Python25\lib\runpy.py", line 95, in run_module
filename, loader, alter_sys)
File "C:\Python25\lib\runpy.py", line 52, in _run_module_code
mod_name, mod_fname, mod_loader)
File "C:\Python25\lib\runpy.py", line 32, in _run_code
exec code in run_globals
File "C:\POP\sound\effects\surround.py", line 5, in <module>
from . import echo
ValueError: Attempted relative import in non-package

Do you want me to post the output from the -vv. I couldn't see that
it actually tried to load it. Here's a relevant portion (I think):
# C:\Python25\lib\linecache.pyc matches C:\Python25\lib\linecache.py
import linecache # precompiled from C:\Python25\lib\linecache.pyc
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Traceback (most recent call last):
File "surround.py", line 1, in <module>
from . import echo
ValueError: Attempted relative import in non-package
# clear __builtin__._

It appears to finish loading everything for python and then
immediately fails due to the import call.
 
D

Daniel

Generally, you want the full version number (my 2.5 is 2.5.4).
However, I set up your demo setup, and found that 2.6.2 worked, and
2.5.4 failed in the same way you see.  I also see that even inside
the effects directory (with 2.6), I can use the syntax to work.
However, 2.5 is nearing "security fixes only" status, so I'd try
moving to 2.6. In the following, "C:\...\Py" is in my standard path.

     C:\...\Py\sound\effects> \python26\python -m sound.effects.surround
     effects package imported
     surround started
     echo imported
     echo in
     formats package imported
     formats in
     filters package imported
     equalizer imported
     filters in

But if I make a file:
     C:\...\Py\> type demo_sound.py
     import sound.effects.surround
     print('all loaded')
I can then:
     C:\...\Py> \python25\python demo_sound.py
     effects package imported
     surround started
     echo imported
     echo in
     formats package imported
     formats in
     filters package imported
     equalizer imported
     filters in
     all loaded
or even:
     C:\...\Py\sound\effects> \python25\python demo_sound.py
     effects package imported
     surround started
     echo imported
     echo in
     formats package imported
     formats in
     filters package imported
     equalizer imported
     filters in
     all loaded

So, not what you want to hear, but at least identified.  I expect
nobody wants to crawl into that code to make your case work,
especially since you can switch to 2.6 and have it work.  The
import code is said to be quite nasty and being rewritten/redocumented.

--Scott David Daniels
(e-mail address removed)

Thanks for doing the experiment. I'll begin to consider testing with
python 2.6 for a possible upgrade.

Daniel
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top