简体   繁体   English

如何在pypy中的.py文件中打开python中的.py文件?

[英]How do I open a .py file in python from a .py file in pypy?

My program currently consists of 2 .py files. 我的程序目前包含2个.py文件。

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 . 我在pypy中运行代码的主要部分(这要快得多),然后在python中打开第二个文件,使用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. 但是,我希望能够将参数传递给在python中打开的plot_function Is that possible? 那可能吗?

Yes, the Popen constructor takes a list of length n. 是的,Popen构造函数采用长度为n的列表。 See the note here. 请参阅此处的注释。 So just add the arguments to main_plot.py to your list: 所以只需将main_plot.py的参数添加到列表中:

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. 您需要修改main_plot.py以接受命令行参数来调用您的函数。 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)

最简单的方法是os.system("main_plot.py arg")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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