0

My program currently consists of 2 .py files.

I run the main part of the code in pypy (which is much faster) and then I open a second file in python that plots my data using matplotlib.pyplot.

I have managed to open the using:

subprocess.Popen(['C:\\python26\\python.exe ','main_plot.py',])

which opens my second file...

import matplotlib.pyplot as pyplot
def plot_function(NUMBER):
    '''some code that uses the argument NUMBER'''
    pyplot.figure()
    ---plot some data---
    pyplot.show()

However, I would like to be able to pass arguments to the plot_function that opens in python. Is that possible?

user1696811
  • 941
  • 4
  • 10
  • 20

2 Answers2

2

Yes, the Popen constructor takes a list of length n. See the note here. So just add the arguments to main_plot.py to your list:

subprocess.Popen(['C:\\python26\\python.exe ','main_plot.py','-n',1234])

EDIT (to respond to your edit):

You need to modify main_plot.py to accept a command line argument to call your function. This will do it:

import matplotlib.pyplot as pyplot
def plot_function(NUMBER):
    '''some code that uses the argument NUMBER'''
    pyplot.figure()
    ---plot some data---
    pyplot.show()

import argparse
if __name__=="__main__":
    argp=argparse.ArgumentParser("plot my function")
    argp.add_argument("-n","--number",type=int,default=0,required=True,help="some argument NUMBER, change type and default accordingly")
    args=argp.parse_args()
    plot_function(args.number)
Rian Rizvi
  • 9,989
  • 2
  • 37
  • 33
  • What I really want to do is pass a number into a function in my plotting file. Is it possible to do this? What is the '-f' used for? – user1696811 Dec 06 '12 at 19:47
  • Thanks, this does exactly what I needed! (I'm not sure what `dbo=DB()` did though. It gave me an error so I deleted it and the program seems to run fine) – user1696811 Dec 06 '12 at 19:59
  • on a side note... If I was to try an run the code on Mac OS, do you know how the `C:\\python26\\python.exe` line would be different? – user1696811 Dec 06 '12 at 20:55
  • 1
    You will just write python without any path on a Mac because it is preinstalled and on the path. But more generally, you use forward slashes instead of backslashes to refer to files on *NIX system eg /Users/yourloginname/somefile – Rian Rizvi Dec 06 '12 at 21:12
0

the easiest way is os.system("main_plot.py arg")

Lotzki
  • 489
  • 1
  • 4
  • 18