我有一个python脚本,它接受一个参数,比如test.py 1
并生成一些数据。第二个脚本experiment.py
重复启动test.py
。我在Spyder IDE中使用runfile
实现了这一点:
所以,experiment.py
读起来像这样:
experiments = {}
for i in range(10):
experiments[str(i)] = {}
runfile(`test.py`, args=str(i), wdir=`/path/to/script/`, namespace=experiments[str(i)])
## some post-processing using data from the dict experiments
这允许我在单独的命名空间中为每个调用存储test.py
的变量。一切都很好,但是(我的问题来了):
如何将runfile
替换为exec
或subprocess.call
,并能够传递命名空间和参数列表?
目标是从命令行而不是Spyder运行脚本。只是为了澄清,我知道执行此类任务的最佳方法是将test.py
转换为函数。然而,我正试图用当前脚本test.py
做,这是相当长的。
1条答案
按热度按时间w8biq8rn1#
修改
test.py
以将其变量保存到文件中,然后在experiment.py
中加载文件,而不是使用exec
使用subprocess.run
,以便您可以从命令行运行脚本,并避免使用特定于Spyder的runfile
函数。test.py
看起来像这样experiment.py