Re: problem with packages and path
- From: Daniel <daniel.watrous@xxxxxxxxx>
- Date: Fri, 29 Aug 2008 14:55:57 -0700 (PDT)
On Aug 29, 1:15 pm, Paul Boddie <p...@xxxxxxxxxxxxx> wrote:
On 29 Aug, 19:08, Daniel <daniel.watr...@xxxxxxxxx> wrote:
I have tried running both commands above from the mypackage directory
and unittests directory. I get the following response universtally.
C:\mypackage>dir
Volume in drive C is Default
Directory of C:\mypackage
08/29/2008 11:04 AM <DIR> .
08/29/2008 11:04 AM <DIR> ..
08/29/2008 11:05 AM <DIR> module1
08/29/2008 11:05 AM <DIR> module2
08/29/2008 11:06 AM <DIR> unittests
08/29/2008 11:04 AM 0 __init__.py
1 File(s) 0 bytes
5 Dir(s) 55,402,070,016 bytes free
If you run unittests\alltests.py from here, sys.pathwill initially
refer to this directory (C:\mypackage) and to anything else set up in
your environment. The extrapathmodification that you put in
alltests.py will append (or insert) the following...
os.path.normpath( os.path.join( __file__, "../../" ))
-> os.path.normpath( os.path.join( "C\\:mypackage\\unittests\
\alltests.py", "../../" ))
-> os.path.normpath( "C\\:mypackage" )
-> "C\\:mypackage"
Since the import statement then tries to access mypackage, which isn't
itself in the C:\mypackage directory - it's in the C:\ directory - the
import fails. The previous, automatically added entry in sys.pathis
duplicated by thepathmodifications shown above, so that won't help
here, either.
C:\mypackage>dir unittests
Volume in drive C is Default
Directory of C:\mypackage\unittests
08/29/2008 11:06 AM <DIR> .
08/29/2008 11:06 AM <DIR> ..
08/29/2008 11:05 AM 0 alltests.py
08/29/2008 11:05 AM 0 test1.py
08/29/2008 11:05 AM 0 test2.py
08/29/2008 11:04 AM 0 __init__.py
4 File(s) 0 bytes
2 Dir(s) 55,401,988,096 bytes free
Here, sys.pathshould refer to C:\mypackage\unittests somewhere, which
won't help Python find mypackage. Thepathmodification in alltests.py
should perform as above, producing C:\mypackage which won't help.
I guess the easiest thing to do is to change thepathmodification
code to use "../../.." which should produce a reference to C:\ instead
of C:\mypackage. The confusion with this particular piece of the code
is the way ".." doesn't cause os.path.join to operate on the directory
C:\mypackage\unittests but instead on the file C:\mypackage\unittests
\alltests.py - that's almost to be expected, however, since
os.path.join doesn't really know that its first argument is really a
file or that ".." is being combined with a file.
What I tend to do, especially with __file__, is to first use
os.path.split to remove the filename - strictly speaking, it's the
leafname - and then to apply the kind of modifications mentioned
above, although there's probably no real difference or, indeed, any
advantage with either approach.
Paul
Thanks Paul. That did the trick.
Daniel
.
- Prev by Date: Re: posix semaphore support?
- Next by Date: Re: logging - how to use in a library?
- Previous by thread: Google License Keys for pyGoogle
- Next by thread: Re: Can I write a binary windows executable with Python?
- Index(es):