Newbie import probelm..

Nuff Said nuffsaid at phreaker.net
Tue Feb 3 07:21:25 EST 2004


On Tue, 03 Feb 2004 06:15:41 -0600, yab wrote:

> Hallo,
> 
> Why does this work when I type it in a t the interpreter prompt;
> 
>>>>class invoker:
> 	def ex(self,cmd):
> 		print 'would do ', cmd
> 
>>>>x=invoker()
> x.ex('test')
> would do  test
> 
> but if I save invoker as invoker.py in a dir that is in sys.path and import it the follwoing happens;
> 
>>>> import invoker
>>>> x=invoker()
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: 'module' object is not callable
>>>>

To make things clearer, let's call your module 'mymod.py' instead of
invoker.py. Now, if you import mymod, you add the name mymod to your
namespace. In order to use the class invoker, you have to write:

    import mymod
    x = mymod.invoker()

If you want to use the class invoker directly, you can write:

    from mymod import invoker
    x = invoker()

The 'from mymod import invoker' adds the name invoker to your namespace.
(Be careful with this; you have to pay attention to possible name clashes
with already defined names in your namespace.)

HTH / Nuff






More information about the Python-list mailing list