471,353 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

relative import question: packaging scripts

What is the recommended packaging of
demo scripts or test scripts for a package
that has modules that use relative imports?

Example:
Suppose I have the package structure:

package/
__init__.py
subpackage1/
__init__.py
moduleY.py
subpackage2/
__init__.py
moduleZ.py

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
structure.)

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 use path manipulation to
find `package`, as suggested by Alex Martelli
http://mail.python.org/pipermail/pyt...ay/438250.html
and others. However it has also been claimed that this approach is an
insane for any shared code. Is it?

I do not want to assume the package will be installed:
a user should be able to play with it without installing it.
In this case, does the only "sane" thing to become to
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?

Thank you,
Alan Isaac
Jun 23 '07 #1
0 1237

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Steve Bishop | last post: by
8 posts views Thread by Jan Danielsson | last post: by
6 posts views Thread by Dan Webb | last post: by
1 post views Thread by schwehr | last post: by
reply views Thread by Anders J. Munch | last post: by
12 posts views Thread by =?Utf-8?B?am9uaWdy?= | last post: by
12 posts views Thread by Alan Isaac | last post: by
reply views Thread by Kay Schluehr | last post: by
7 posts views Thread by Robert Hancock | 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.