Packages and modules

Discussion in 'Python' started by Dan Richter, Jul 26, 2005.

  1. Dan Richter

    Dan Richter Guest

    I'm trying to create a package+module structure, specifically a "test"
    package with all the unit tests. I'd like to have a package (directory)
    "test" that has various test modules, and I'd also like "test" itself to
    be a module that runs all the tests. Is this not possible?

    I created a directory called "test" with an __init__.py file that
    defines a variable __all__. That works, but no executable code in
    __init__.py is executed, even though "import test" seems to succeed. I
    have confirmed that it's not a naming conflict (i.e., there's not some
    other Python module also named "test").
     
    Dan Richter, Jul 26, 2005
    #1
    1. Advertisements

  2. Dan Richter

    Peter Hansen Guest

    Are you certain? The way to check is with "test.__file__" after
    importing test. There _is_ a standard library package called test, and
    when I import test here and do this test I get:
    'c:\\python24\\lib\\test\\__init__.pyc'

    -Peter
     
    Peter Hansen, Jul 26, 2005
    #2
    1. Advertisements

  3. Dan Richter

    Dan Guest

    no executable code in
    I've discovered that "import test" *does* cause executable code in the
    package to be executed. However, I can't execute it on the command line
    using "python test". Is there a way to do this?
    Oh, you're right. But I've renamed the module to XYZ and I still have
    the problem.
     
    Dan, Jul 26, 2005
    #3
  4. Dan Richter

    Peter Hansen Guest

    Using the latest version of Python, "python -m test" should do it,
    though I don't know if that works for packages, or just modules. Hang
    on... appears to work only for modules.

    Okay, this should be universal, if slightly more awkward:

    python -c "import xyz"

    The problem with this approach is that it won't execute the "if __name__
    == '__main__':" code at the end, so if you want to execute a particular
    function directly, just add the call manually:

    python -c "import xyz; xyz.main()"
    Which problem?
     
    Peter Hansen, Jul 26, 2005
    #4
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.