简体   繁体   中英

Running external program using pipes and passing arguments in python

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
#(...)

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.

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.

  1. Use Popen 's env parameter to pass environment variables:
  2. Don't use shell=True unless you have to. It can be asecurity 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM