Usual practice: running/testing modules in a package

Gabriel Genellina gagsl-py2 at
Fri Aug 22 19:25:35 CEST 2008

En Fri, 22 Aug 2008 10:48:50 -0300, med.swl at <med.swl at>  

> On 18 ago, 08:28, "Gabriel Genellina" <gagsl-... at> 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 package.

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 in the same directory? what if contains code? when  
should it be executed?

Gabriel Genellina

More information about the Python-list mailing list