relative import question: packaging scripts

Discussion in 'Python' started by Alan Isaac, Jun 23, 2007.

  1. Alan Isaac

    Alan Isaac Guest

    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/python-list/2007-May/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
    Alan Isaac, Jun 23, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Symon
    Replies:
    1
    Views:
    1,548
  2. Jan Danielsson
    Replies:
    8
    Views:
    620
    Mike Meyer
    Jul 22, 2005
  3. Jp Calderone
    Replies:
    0
    Views:
    449
    Jp Calderone
    Jul 21, 2005
  4. Alex Hall
    Replies:
    4
    Views:
    667
  5. Boxuan Cui
    Replies:
    0
    Views:
    85
    Boxuan Cui
    Jun 25, 2013
Loading...

Share This Page