0

I have 2 modules

mexec1.py

def exec1func():
    print 'exec1'
    exec 'c:/python27/exec2.py'

if __name__ == '__main__':
    exec1func()

exec2.py

def exec2func(parm=''):
    print 'exec2 parm',parm

if __name__ == '__main__':
    exec2func(parm='')

From exec1.py I want to call exec2func of the exec2.py using only exec or execfile...I don't want subprocess.Popen..

0

2 Answers 2

2

Use import instead:

def exec1func():
    from exec2 import exec2func
    exec2func()

If you want to import using the full path, use imp.load_source:

import imp

def exec1func():
    exec2 = imp.load_source('exec2', 'c:/python27/exec2.py')
    exec2.exec2func()
Sign up to request clarification or add additional context in comments.

Comments

0

It would be better to make it a module and import it. If you need dynamic imports use importlib.

mod = importlib.import_module("exec2", package=None)
mod.exec2func()

1 Comment

@nneonneo That method does not deal with packages. But for a base module it would work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.