tags:

views:

39

answers:

3

Hi all,

I am a having two python files file1.py and file2.py. I am using exec() to get the method/Variables defined in the file2.py.

file1.py have a class as given below
class one:
       def __init__(self):
            self.HOOK = None
            exec(file2.py)
            self.HOOK = Generate
            ### call the hook method ####
            self.HOOK()

file2.py looks like as (There is no class define in file2.py)
def Generate()
        do 1
        do 2
        hello()

def Hello()
     print "hello"

Now the problem is as When i run script it is giving a error global name Hello not found. If i remove Hello() from Generate method in file2.py then its work fine. I cant use import file2.py in file1.py,because in file2.py the only one method name (Generate) is fix (its taken as requirement). So apart from Genarate method user can define any method and can call this in generate method, because this approach is not working so i have to write whole code into generate method only and code is also repetitive.

Any help is really appreciable...

A: 

From what you have posted, it looks like you define a function

def hello():
    print "hello"

and then tries to call another function

Hello()

Notice that python cares a lot about capital and noncapital letters.

Thomas Ahle
My apologies for that mistake...This is typos only
mukul sharma
A: 

You can safely write from file2 import Generate in file1.py, that will do the trick - it will import only the Generate method from file2 into the current namespace, so all the other methods the user has defined in file2 won't clutter the namespace of file1.

Tamás
Hi Tamas, I think you misinterpreted my problem.I want when you cal Generate() then in my current space no method Hello() is defined. but i want that Hello() also in my current name space.
mukul sharma
so how should i exec that all other method called inside Generate() will called also
mukul sharma
@mukul: So you're trying to do some kind of auto-import? But at least read up here: http://stackoverflow.com/questions/1493888/python-auto-importing
Xavier Ho
Yes Xavier that is my main object. I just want to set only minimum constraint to the user and rest of thing i shd handle. In my case like user have to write only one method Generate() in file2.py. rest of the name he can define whatever he want and call inside them to the Generate().
mukul sharma
Is there is no way i can do this by exec?
mukul sharma
@mukul: There is a way. You can look into `__import__`: http://docs.python.org/library/functions.html#__import__ || Just FYI, what you're doing is a very unusual thing, and isn't exactly the best practice sometimes.
Xavier Ho
+1  A: 

Try this:

# file1.py
from file2 import Generate

class one:
   def __init__(self):
       self.HOOK = Generate
       ### call the hook method ####
       self.HOOK()

In your second file:

# file2.py
def Generate():
    # do 1
    # do 2
    hello()

def hello()
    print "hello"
Xavier Ho