Packages and modules

D

Dan Richter

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

Peter Hansen

Dan said:
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:
'c:\\python24\\lib\\test\\__init__.pyc'

-Peter
 
D

Dan

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

Peter Hansen

Dan said:
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()"
Oh, you're right. But I've renamed the module to XYZ and I still have
the problem.

Which problem?
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top