473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

problem with packages and path

Hello,

I'm writing some unit tests for my python software which uses
packages. Here is the basic structure:

mypackage
__init__.py
module1
__init__.py
mod1.py
module2
__init__.py
mod2.py
unittests
__init__.py
alltests.py
test1.py
test2.py

within alltests.py I would expect to be able to "import
mypackage.unittests.test1". In fact within PyScripter this works as
expected. However, when I execute the code from the command line, I
get the following error:

ImportError: No module named mypackage.unittests.test1

I've read that "When importing the package, Python searches through
the directories on sys.path looking for the package subdirectory." -
http://docs.python.org/tut/node8.htm...00000000000000

So I tried adding the following code to my alltests.py, which should
add the directory for mypackage to the path (and it does).

import os, sys
newpath = os.path.normpath( os.path.join( __file__, "../../" ))
sys.path.append(newpath)

I still get the same error. Can someone please point me in the right
direction? Thanks in advance:

Daniel
Aug 27 '08 #1
7 1715
On 27 Aug, 18:44, Daniel <daniel.watr...@gmail.comwrote:
>
I'm writing some unit tests for my python software which uses
packages. *Here is the basic structure:

mypackage
[...]
* unittests
* * __init__.py
* * alltests.py
* * test1.py
* * test2.py

within alltests.py I would expect to be able to "import
mypackage.unittests.test1". *In fact within PyScripter this works as
expected. *However, when I execute the code from the command line, I
get the following error:

ImportError: No module named mypackage.unittests.test1
One thing to note: if you are running alltests.py directly (which you
don't mention explicitly) then even if you do so from the directory
containing the root of the package hierarchy (mypackage), it will be
the directory containing alltests.py (unittests) which will appear on
the PYTHONPATH/sys.path. Sometimes it's easy to take this behaviour
for granted when running programs.

A question: what happens if you prepend the directory containing the
root of package hierarchy to sys.path using insert (at element 0)
rather than append?

Paul
Aug 27 '08 #2
On Aug 27, 11:00*am, Paul Boddie <p...@boddie.org.ukwrote:
On 27 Aug, 18:44, Daniel <daniel.watr...@gmail.comwrote:
I'm writing some unit tests for my python software which uses
packages. *Here is the basic structure:
mypackage

[...]
* unittests
* * __init__.py
* * alltests.py
* * test1.py
* * test2.py
within alltests.py I would expect to be able to "import
mypackage.unittests.test1". *In fact within PyScripter this works as
expected. *However, when I execute the code from the command line, I
get the following error:
ImportError: No module named mypackage.unittests.test1

One thing to note: if you are running alltests.py directly (which you
don't mention explicitly) then even if you do so from the directory
containing the root of the package hierarchy (mypackage), it will be
the directory containing alltests.py (unittests) which will appear on
the PYTHONPATH/sys.path. Sometimes it's easy to take this behaviour
for granted when running programs.

A question: what happens if you prepend the directory containing the
root of package hierarchy to sys.path using insert (at element 0)
rather than append?

Paul
I changed it to 'sys.path.insert(0, newpath)', as you suggest, but it
doesn't fix the error.

I did notice that the directory containing the file alltests.py ends
up in the path. I've also noticed that (in WinXP at least) the
sys.path modification inserts a '..' in the path, rather than the full
path to 'mypackage'. I'm not sure if this makes a difference, but it
seems like I should be able to add either '..' or 'path/to/mypackage'
to the path and have it find my packages.

Any more suggestions?
Aug 27 '08 #3
On Aug 27, 11:00*am, Paul Boddie <p...@boddie.org.ukwrote:
On 27 Aug, 18:44, Daniel <daniel.watr...@gmail.comwrote:
I'm writing some unit tests for my python software which uses
packages. *Here is the basic structure:
mypackage

[...]
* unittests
* * __init__.py
* * alltests.py
* * test1.py
* * test2.py
within alltests.py I would expect to be able to "import
mypackage.unittests.test1". *In fact within PyScripter this works as
expected. *However, when I execute the code from the command line, I
get the following error:
ImportError: No module named mypackage.unittests.test1

One thing to note: if you are running alltests.py directly (which you
don't mention explicitly) then even if you do so from the directory
containing the root of the package hierarchy (mypackage), it will be
the directory containing alltests.py (unittests) which will appear on
the PYTHONPATH/sys.path. Sometimes it's easy to take this behaviour
for granted when running programs.

A question: what happens if you prepend the directory containing the
root of package hierarchy to sys.path using insert (at element 0)
rather than append?

Paul
I changed it to 'sys.path.insert(0, newpath)', as you suggest, but it
doesn't fix the error.

I did notice that the directory containing the file alltests.py ends
up in the path. I've also noticed that (in WinXP at least) the
sys.path modification inserts a '..' in the path, rather than the full
path to 'mypackage'. I'm not sure if this makes a difference, but it
seems like I should be able to add either '..' or 'path/to/mypackage'
to the path and have it find my packages.

Any more suggestions?
Aug 27 '08 #4
On Wed, Aug 27, 2008 at 6:44 PM, Daniel <da************@gmail.comwrote:
Hello,

I'm writing some unit tests for my python software which uses
packages. Here is the basic structure:

mypackage
__init__.py
module1
__init__.py
mod1.py
module2
__init__.py
mod2.py
unittests
__init__.py
alltests.py
test1.py
test2.py

within alltests.py I would expect to be able to "import
mypackage.unittests.test1". In fact within PyScripter this works as
expected. However, when I execute the code from the command line, I
get the following error:

ImportError: No module named mypackage.unittests.test1
1) What is the command you're using to run the alltest.py module?

2) what is the result of:
- python -c "import mypackage"
- python -c "import mypackage.unittests"

Regards
Marco

--
Marco Bizzarri
http://iliveinpisa.blogspot.com/
Aug 28 '08 #5
On Aug 28, 2:28*am, "Marco Bizzarri" <marco.bizza...@gmail.comwrote:
On Wed, Aug 27, 2008 at 6:44 PM, Daniel <daniel.watr...@gmail.comwrote:
Hello,
I'm writing some unit tests for my python software which uses
packages. *Here is the basic structure:
mypackage
*__init__.py
*module1
* *__init__.py
* *mod1.py
*module2
* *__init__.py
* *mod2.py
*unittests
* *__init__.py
* *alltests.py
* *test1.py
* *test2.py
within alltests.py I would expect to be able to "import
mypackage.unittests.test1". *In fact within PyScripter this works as
expected. *However, when I execute the code from the command line, I
get the following error:
ImportError: No module named mypackage.unittests.test1

1) What is the command you're using to run the alltest.py module?

2) what is the result of:
* *- python -c "import mypackage"
* - python -c "import mypackage.unittests"

Regards
Marco

--
Marco Bizzarrihttp://iliveinpisa.blogspot.com/
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

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

C:\mypackage>python -c "import mypackage"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named mypackage

C:\mypackage>python -c "import mypackage.unittests"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named mypackage.unittests
Aug 29 '08 #6
On 29 Aug, 19:08, Daniel <daniel.watr...@gmail.comwrote:
>
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.path will initially
refer to this directory (C:\mypackage) and to anything else set up in
your environment. The extra path modification 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.path is
duplicated by the path modifications 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.path should refer to C:\mypackage\unittests somewhere, which
won't help Python find mypackage. The path modification in alltests.py
should perform as above, producing C:\mypackage which won't help.

I guess the easiest thing to do is to change the path modification
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
Aug 29 '08 #7
On Aug 29, 1:15*pm, Paul Boddie <p...@boddie.org.ukwrote:
On 29 Aug, 19:08, Daniel <daniel.watr...@gmail.comwrote:


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
Aug 29 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jose Vicente Nunez Z | last post by:
Greetings, I wrote a couple of custom dummy extensions in Python (one a pure Python and the other a C) and i managed to compile and install them without a problem: $ make python2 setup.py...
3
by: ketulp_baroda | last post by:
Hi I am having a problem in appending to sys.path I am doing it this way: >>> sys.path >>> sys.path.append(r'D:\Python23\Lib\site-packages\code') >>> sys.path But when I close the shell...
0
by: Remy C. Cool | last post by:
Hello, My application uses a remote import scheme using the sys.path_hooks solution as explained in PEP302 (http://www.python.org/peps/pep-0302.html) The importer works fine for packages,...
1
by: aznach | last post by:
Hello! I have a shared hosting account at GrokThis.net and have a problem with the module order of the Python search path. I'd like to use django's svn trunk instead of the...
7
by: siggi | last post by:
Hi all, when I do >>>sys.path in IDLE (winXP), i get a horrendously long list of paths, paths I may have used during a lot of trials and errors. How can I clean up sys.path? I mean, trim it of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.