Usual practice: running/testing modules in a package
gagsl-py2 at yahoo.com.ar
Fri Aug 22 19:25:35 CEST 2008
En Fri, 22 Aug 2008 10:48:50 -0300, med.swl at gmail.com <med.swl at gmail.com>
> On 18 ago, 08:28, "Gabriel Genellina" <gagsl-... at yahoo.com.ar> wrote:
>> A package is a library, meant to be imported by some other code. Your
>> main script (or the testing code) is a program, it uses (i.e. imports)
>> the library.
> You are right that a module is a library and its main use is to be
> imported from another module or program. But is a common practice in
> Python to introduce testing code as part as the module itself. Even
> there is an option “-m” to do that.
> So (in my humbled opinion), that is one error Python must fix.
A *module* or a *package*? The OP asked how to test a package, and a
package is clearly a library. In the final application, it will be
imported. So the test code should mimic the same environment, and import
The easiest way to import a package is to run a script from its container
directory: the current directory is already in sys.path so the package
directory is found directly without messing with sys.path, PYTHONPATH
variable and such. This is my suggested approach, but there are many other
alternatives. Anyway, the idea is to import the package to test it.
If you don't import the package and just run some script from inside it,
how would Python know that it belongs to a package? Looking for a
__init__.py in the same directory? what if __init__.py contains code? when
should it be executed?
More information about the Python-list