471,350 Members | 1,633 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

cannot have test scripts in packages?

This is really a repackaging of an earlier question,
probably illustrating that I still do not understand relative imports.

Suppose I have the package structure (taken from the example at


Important detail:
moduleZ uses a relative import to access moduleY.

The problem:
I have a script test.py that I want to
distribute with the package. It will import
moduleZ to illustrate or test the module's use.

Is it the case that this script cannot reasonably be
bundled with `package`? (I.e., within its directory

I cannot put it in the `subpackage2` directory and
just import moduleZ, because then I will get
ValueError: Attempted relative import in non-package

I cannot put it in the `package` directory and
import subpackage2.moduleZ, because then I will get
ValueError: Attempted relative import beyond toplevel package

The script could then use path manipulation to
find `package`, as suggested by Alex Martelli
and others. However it has also been claimed that this approach is an
insane for any shared code.

Btw, I do not want to assume the package will be installed:
a user should be able to play with it without installing it.

So if the package is not installed, is the only "sane"
thing to do is require any user to take the step of
inserting the package location into sys.path and have
test.py rely on the user having done this?

What is the recommended handling of demo or test scripts
for a package?

Thank you,
Alan Isaac
Jun 22 '07 #1
0 1099

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Michele Simionato | last post: by
1 post views Thread by George van den Driessche | last post: by
27 posts views Thread by Josh | last post: by
3 posts views Thread by David T. Ashley | last post: by
6 posts views Thread by Mendis | last post: by
6 posts views Thread by sam | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.