RM1970 RM1970 - 1 year ago 64
Python Question

can't call an existing function from python shell

I have a python/django project, with a django app called

"testApp"
. under
"testApp"
I have a python file:
"hello.py"
. The file reads:

class Hello():
def sayHello():
print ("hello")


How can I activate the
sayHello
function from
python manage.py shell


importing
"testApp"
doesn't help as calling
"Hello.sayHello()"
gives out an
undefined
error.

What am I doing wrong?

Answer Source

You need to import the actual name you want to use. Just importing testApp won't do anything; you would need to do from testApp.hello import Hello.

Note also that since for some reason you have a class, sayHello is an instance method and therefore needs to accept the self parameter; also, you would need to instantiate the class before calling the method. However, Python is not Java, and there is absolutely no need for the Hello class to exist.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download