Hello hello,

I am writing to you because I am having trouble obtaining something: I know it should be doable, but can't seem to make an example.

I need to be able to run scripts from another central script. And as i' ve been reading, apparently they mustn't be imported as modules because the __name__ attribute of the module changes...etc.

The problem is that I want to have a class instance I make in my central script available in the rest. Like this:

CentralScript.py

class Mine:
    def __init__(self, x, y):
        self.X = x
        self.Y = y

    def PrintMe(self):
        print 'My X: ',self.X
        print 'My Y: ',self.Y

    def ChangeMe(self, X, Y):
        self.X = X
        self.Y = Y


m = Mine(2,3)
m.PrintMe()
m.ChangeMe(10,11)
m.PrintMe()

#Call to script SecondaryScript here....

The problem seems obvious, m is not known in the SecondaryScript.py

SecondaryScript.py

m.ChangeMe(100,101)
m.PrintMe()

And somehow I've heard that it works...Do you have any suggestions as for the mistakes I'm making?

I'd appreciate it very much, thanks...
T

Recommended Answers

All 5 Replies

Pass m to the second script as a function parameter or to the __init__() function of the class. If that doesn't do what you want, post back.

m = Mine(2,3)
m.PrintMe()
m.ChangeMe(10,11)
m.PrintMe()

#Call to script SecondaryScript here.... 
SecondaryScript.foo(m)
SecondaryScript.ClassFoo(m)

I'm not exactly sure I understand what you're suggesting... Is it importing SecondaryScript? And calling functions from it and passing m as parameter?

If it is that, I've tried it at the beginning and it works, if I bundle my m functions in a function in the script, and I import it elsewhere having m as parameter to the function, it works like a charm.

I was asking if there is a way to execute the script , not import , execute its code like...like the __inline does for C: like pasting the code inside the calling script. Just order its code to be read and not import it as a module object....

Am i making any sense?...

Sorry, I misunderstood. The only way I know of is to pass it as an arg to the script being executed. Something like:
subprocess.call( 'program_name m', shell=True)
The program being executed would have to examine sys.argv to find it.

Sorry, I misunderstood. The only way I know of is to pass it as an arg to the script being executed. Something like:
subprocess.call( 'program_name m', shell=True)
The program being executed would have to examine sys.argv to find it.

This would not allow him to use the class members or methods as program_name m in the shell would not make anysense and the argv[1] would simply contain a character m.

Perhaps you could pickle the class, pass the name of the pickled file via the above method, and then unpickle in the target script?

Refer to pickling docs for more info.

Yes, m is a pointer to the class so could not be passed. Perhaps an explaination of what you are trying to do would help. There may be other solutions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.