Newbie import probelm..

Y

yab

Hallo,

Why does this work when I type it in a t the interpreter prompt;
def ex(self,cmd):
print 'would do ', cmd
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;
Traceback (most recent call last):
 
D

Diez B. Roggisch

import invoker
Traceback (most recent call last):

Try

x=invoker.invoker()

The thing is that you declared the _class_ invoker in a file also invoker.
That makes

import invoker

create a module named invoker, where all the things defined in invoker.py
are located. Try

Regards,

Diez
 
N

Nuff Said

Hallo,

Why does this work when I type it in a t the interpreter prompt;

def ex(self,cmd):
print 'would do ', cmd

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;

Traceback (most recent call last):

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
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top