1

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...

2
  • Please format your code as code (starting every line with four spaces). Then it will be much easier to read on stack overflow. Commented May 28, 2010 at 6:35
  • Is there a reason you need to use exec()? Usually there is a workaround. Commented May 28, 2010 at 10:31

3 Answers 3

1

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"
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

My apologies for that mistake... This is typos only
0

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.

6 Comments

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.
so how should i exec that all other method called inside Generate() will called also
@mukul: So you're trying to do some kind of auto-import? But at least read up here: stackoverflow.com/questions/1493888/python-auto-importing
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().
Is there is no way i can do this by exec?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.