ActiveState Python won't call module function.

Logan logan at phreaker.nospam
Sat Nov 29 21:57:06 CET 2003


On Sun, 30 Nov 2003 08:56:22 +1300, Glenn Reed wrote:

> ------- module1.py -------------
> class module2:
>     def __init__(self):
>         self.x=23
>         self.y=14
>         self.z=33
>         self.x1=self.x=3
> 
> ------- tmod.py ---------------
> # Test Module
> import module1
> 
> thisClass = module2()
> 
> ----------------------------------------------
> 
> It generates the following error:
> NameError: name 'module2' is not defined

'import module1' adds the name 'module1' to your namespace;
so, the name 'module2' is still unknown in your namespace.

In the above example, you have to write:

    thisClass = module1.module2()

Alternatively, you might prefer the following:

    from module1 import module2

    thisClass = module2()

If you want to import more than just one class, the following
syntax will do this:

    from module1 import module2, module3, module4

By the way: your naming conventions (module1 for the file and
module2 for the class) are not really intuitive :-)

HTH, L.


-- 
mailto: logan at phreaker(NoSpam).net





More information about the Python-list mailing list