Replace runfile with exec/subprocess.call while passing a namespace.

huangapple go评论66阅读模式
英文:

Replace runfile with exec/subprocess.call while passing a namespace

问题

You can replace the use of runfile with exec and subprocess.call to achieve your objective of running the script from the command line. Here's how you can do it:

Using exec:

experiments = {}
for i in range(10):
    experiments[str(i)] = {}
    namespace = experiments[str(i)]
    exec(open('test.py').read(), namespace)
    # Now you can access variables from 'test.py' via 'namespace'

## Perform post-processing using data from the 'experiments' dictionary

Using subprocess.call:

import subprocess

for i in range(10):
    subprocess.call(['python', 'test.py', str(i)])

With these changes, you can run your script from the command line while passing arguments and still store variables in the 'experiments' dictionary for further processing.

英文:

I have a python script that accepts an argument, say test.py 1 and generates some data. A second script experiment.py launches test.py repeatedly. I achieve this using runfile in Spyder IDE:

So, experiment.py reads something like:

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

This allows me to store variables of test.py for each call in a separate namespace. Everything works well, but (here comes my question):

How can I replace runfile with exec or subprocess.call and be able to pass a namespace and an argument list?

The objective is to run the script from command line rather than Spyder. Just to clarify, I am aware that the best way to perform such a task is to convert test.py into a function. However, I am trying to make-do with the current script test.py which is quite long.

答案1

得分: 0

修改 test.py 以将其变量保存到文件中,然后在 experiment.py 中加载文件,而不是使用 exec,而是使用 subprocess.run,以便您可以从命令行运行脚本,避免使用特定于 Spyder 的 runfile 函数。

test.py 将如下所示:

import sys
import json

arg = int(sys.argv[1])

# 在这里插入您的原始代码

variables_to_save = {'variable1': variable1, 'variable2': variable2}
with open(f"variables_{arg}.json", "w") as f:
    json.dump(variables_to_save, f)

以及 experiment.py

import subprocess
import json

experiments = {}
for i in range(10):
    experiments[str(i)] = {}

    subprocess.run(["python", "test.py", str(i)], cwd="/path/to/script/")

    with open(f"variables_{i}.json", "r") as f:
        experiments[str(i)] = json.load(f)

请注意,您需要将 "variable1""variable2" 替换为要保存的实际变量名称。

英文:

Modify test.py to save its variables to a file and then loading the file in experiment.py instead of using exec use subprocess.run so that you can run the script from the command line and avoid using the Spyder-specific runfile function.

test.py will look like this

import sys
import json

arg = int(sys.argv[1])

# Your original code here

variables_to_save = {'variable1': variable1, 'variable2': variable2}
with open(f"variables_{arg}.json", "w") as f:
    json.dump(variables_to_save, f)

and experiment.py

import subprocess
import json

experiments = {}
for i in range(10):
    experiments[str(i)] = {}
    
    subprocess.run(["python", "test.py", str(i)], cwd="/path/to/script/")
    
    with open(f"variables_{i}.json", "r") as f:
        experiments[str(i)] = json.load(f)

huangapple
  • 本文由 发表于 2023年4月19日 18:00:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定