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

    --
    I had picked out the theme of the baby's room and done other
    things. I decided to let Jon have this.
    - Jamie Cusack (of the Netherlands), whose husband Jon
    finally talked her into letting him name their son Jon 2.0
     
    Dan Richter, Jul 26, 2005
    #1
    1. Advertising

  2. Dan Richter

    Peter Hansen Guest

    Dan Richter wrote:
    > 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").


    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:

    >>> test.__file__

    'c:\\python24\\lib\\test\\__init__.pyc'

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

  3. Dan Richter

    Dan Guest

    > > no executable code in
    > > __init__.py is executed, even though "import test" seems to succeed.


    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?

    > There _is_ a standard library package called test


    Oh, you're right. But I've renamed the module to XYZ and I still have
    the problem.

    --
    They had a big meeting, drank some beer and had some pizza and
    decided 'A' would be 65.
    - Jim Greenly, professor at Georgia Institute of Technology
     
    Dan, Jul 26, 2005
    #3
  4. Dan Richter

    Peter Hansen Guest

    Dan wrote:
    >>>no executable code in
    >>>__init__.py is executed, even though "import test" seems to succeed.

    >
    > 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?


    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()"

    >>There _is_ a standard library package called test

    > Oh, you're right. But I've renamed the module to XYZ and I still have
    > the problem.


    Which problem?
     
    Peter Hansen, Jul 26, 2005
    #4
    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. Paul  Smith
    Replies:
    0
    Views:
    734
    Paul Smith
    Nov 18, 2003
  2. Replies:
    2
    Views:
    256
    Steve Holden
    Nov 10, 2006
  3. David Lyon
    Replies:
    19
    Views:
    583
    David Lyon
    Apr 24, 2009
  4. Pascal Chambon
    Replies:
    0
    Views:
    260
    Pascal Chambon
    Mar 20, 2010
  5. Ian
    Replies:
    3
    Views:
    171
Loading...

Share This Page