0

This is how my code looks and I get an error, while using Popen

test.py

import subprocess import sys def test(jobname): print jobname p=subprocess.Popen([sys.executable,jobname,parm1='test',parm2='test1']) if __name__ == "__main__": test(r'C:\Python27\test1.py') 

test1.py

def test1(parm1,parm2): print 'test1',parm1 if __name__ = '__main__': test1(parm1='',parm2='') 

Error

Syntax error

1 Answer 1

4

In test1.py:

You need two equal signs in :

if __name__ = '__main__': 

Use instead

if __name__ == '__main__': 

since you want to compare the value of __name__ with the string '__main__', not assign a value to __name__.


In test.py:

parm1='test' is a SyntaxError. You can not to assign a value to a variable in the middle of a list:

p=subprocess.Popen([sys.executable,jobname,parm1='test',parm2='test1']) 

It appears you want to feed different values for parm1 and parm2 into the function test1.test1. You can not do that by calling python test1.py since there parm1='' and parm2='' are hard-coded there.

When you want to run a non-Python script from Python, use subprocess. But when you want to run Python functions in a subprocess, use multiprocessing:

import multiprocessing as mp import test1 def test(function, *args, **kwargs): print(function.__name__) proc = mp.Process(target = function, args = args, kwargs = kwargs) proc.start() proc.join() # wait for proc to end if __name__ == '__main__': test(test1.test1, parm1 = 'test', parm2 = 'test1') 
Sign up to request clarification or add additional context in comments.

2 Comments

It should probably be Popen([sys.executable, jobname, 'test', 'test1']). test1.py needs to use sys.argv.
@eryksun: Thanks; you are absolutely right. In that case, I think multiprocessing in test.py would be cleaner than changing test1.py. Editing...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.