473,322 Members | 1,736 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,322 software developers and data experts.

Question about packages

djw
I'm sturcturing my (relatively) large application into packages and am
having trouble understanding one aspect of module/package paths.

If I have a structure like:

in /:
appA.py
__init__.py

in X/:
modX1.py
modX2.py
__init__.py

in Y:/
modY1.py
__init__.py

Now, if I do the following imports and run appA.py, all is well:

in appA.py:
from X import modX1
import Y.modY1

in modX1:
import modX2

in modY1: (as long as run from appA.py)
import X.modX1

But, if I try to create a test harness inside modY1 (in an if statement
checking __name__ with "__main__") and run modY1.py with the last import
(import X.modX1), I get an ImportError.

So, the question is, how do you import into a sub-package to allow both
a script at the root level to import the module ("library mode") and for
the module itself to run in "test harness" mode with the same import?

Any hints appreciated.

-Don




Jul 18 '05 #1
1 1165
On Mon, 2003-12-29 at 21:44, djw wrote:
So, the question is, how do you import into a sub-package to allow both
a script at the root level to import the module ("library mode") and for
the module itself to run in "test harness" mode with the same import?


You could do something like:

try:
import X.modX1
except ImportError:
if __name__ == '__main__':
# Hmm, must be running the test harness, adjust
# sys.path appropriately, then retry import
import sys
sys.path.insert(0, whatever)
import X.modX1

but that smells.

Maybe it's best to separate tests from code?

Cheers,

// m
Jul 18 '05 #2

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

Similar topics

1
by: Ksenia Marasanova | last post by:
Hi list, I'd appreciate any advice on the following: In the "live" environment, I have some Python scripts which make use of Python packages. The directory structure is something like that:...
0
by: Tian | last post by:
I am trying to use pyFMOD, I have installed all other supporting libraries (hopefully), such as ctypes, numarray, I also installed FMOD itself and copied its DLL files to python/DLLs. When I try to...
0
by: Noelle QUEMENER | last post by:
I have just installed BLT: I effectively had some problems - the same you had. I found this: Python GUI Setup Here is the procedure I used to get "Fourier" working on Windows and Linux. I...
1
by: Greg Wilson | last post by:
I'm trying to convert from minidom to ElementTree for handling XML, and am having trouble with entities in DTDs. My Python script looks like this: ...
2
by: John Pollins | last post by:
Greetings, We are trying to set up a set of "Leading Practices" for our developers, as well as ourselves, and hope some gentle reader can recommend some documentation in favor of what appears to...
2
by: Josh Mcfarlane | last post by:
I've create a base class for Packages that I am sending via TCP/IP, and then deriving the different transport classes from this base class. Now I can recreate the classes on the other end with...
1
by: Peter Hartmann | last post by:
How do I influence the platform type during install? Could you look at this and tell me what I'm doing wrong? It's still using information from get_platform instead of using my preference. #...
0
by: giovanni gherdovich | last post by:
Hello, first of all: Is this the right place to ask plastek-related questions? I'm trying to make plastex work on my Ubuntu Dapper Drake. For LaTeX, I have the all-in-one package tetex.
0
by: Laszlo Nagy | last post by:
Hi All! I just installed ZSI and tried to generate client code for a wsdl. Here is the exception I got: Traceback (most recent call last): File "/usr/local/bin/wsdl2py", line 9, in ?...
6
by: Daniel | last post by:
I hope this question is OK for this list. I've downloaded Rpyc and placed it in my site packages dir. On some machines it works fine, on others not so much. Here is one error I get when I try...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.