1

I tried that, but when I try to print these arguments it returns no values. I submit my code below:

script1 that runs external python program (script2)

#(...)
proc = subprocess.Popen(['export PYTHONPATH=~/:$PYTHONPATH;' +
    'export environment=/path/to/environment/;' +
    'python /path/to/my/program/myProgram.py',
    '%(date)s' %{'date':date}, '%(time)s' %{'time':time}],
    stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
#(...)

script2 that is being run by the script1

#(...)
print sys.argv[0] #prints the name of the command
print sys.argv[1] #it suppose to print the value of the first argument, but it doesn't
print sys.argv[2] #it suppose to print the value of the second argument, but it doesn't
#(...)
2
  • have you tried using shlex.split ? Commented Aug 9, 2011 at 21:47
  • no, not really, would that help? Commented Aug 9, 2011 at 22:11

3 Answers 3

1

Try this version of script 1:

proc = subprocess.Popen('python /path/to/my/program/myProgram.py %s %s' % (date, time),
                        stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True,  
                        env = {'PYTHONPATH': '~/:$PYTHONPATH',
                               'environment': '/path/to/environment/'})

It should make it easier to find your problem if it doesn't work; but I think it will.

Sign up to request clarification or add additional context in comments.

3 Comments

unfortunately it didn't work, in fact it wouldn't even print the output of script2
Did you try it after my edit? And did you try it WITHOUT the =PIPE arguments?
I just tried without =PIPE arguments, but it throws error, as it cannot import library, so it probably didn't set the envelope correctly
1

Docs say that when specifying shell=True any additional args are treated as args to the shell, not to the command. To make it work, just set shell to False. I don't see why you need it to be True.

edit: I see you want to use shell to set environment variables. Use the env argument to set the environment variables instead.

3 Comments

The other two answers address important issues, but this is the main problem.
I need shell to be true, as I need to export libraries first
Like I said. Use env argument to set the environment instead. If you need to copy any environment variables from your current environment then copy environ in the os module.
1
  1. Use Popen's env parameter to pass environment variables:
  2. Don't use shell=True unless you have to. It can be a security risk (see Warning).

test.py:

import subprocess
import shlex
import datetime as dt
now=dt.datetime.now()
date=now.date()
time=now.strftime('%X')

proc = subprocess.Popen(shlex.split(
    'python /tmp/test2.py %(date)s %(time)s'%{'date':date,
                                         'time':time}),
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE,
                        env={'PYTHONPATH':'~/:$PYTHONPATH',
                             'environment':'/path/to/environment/'})

out,err=proc.communicate()
print(out)
print(err)

test2.py:

import sys
import os

print(os.environ['PYTHONPATH'])
print(os.environ['environment'])
for i in range(3):
    print(sys.argv[i])

yields

~/:$PYTHONPATH
/path/to/environment/
/tmp/test2.py
2011-08-09
17:50:04

1 Comment

If print sys.argv[0] is working, then the pipes aren't his problem. Good call on the env parameter.

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.