0

I have my Python code to run a simulation stored in a file called script. This script takes a lot of input parameters that will be used to run the simulation. Now, I want to separate the definitions of these input parameters from the actual code that does the simulation because I might want to run many simulations with different sets of inputs (corresponding to different physical situations, for example). And the file where these inputs are defined will in general not be in the same folder as script, but I will 'feed' this input to the script from the command line in this way: script input.py (there is this line: /path/to/python python at the top of script). The style the inputs are defined inside input.py is nothing fancy, it is all that any Python interpreter understands, e.g. var = value if its a scalar parameter, or var = [value1, value2, value3] if it's a list, etc. So, what I want is basically to copy-paste the content of input.py to script before running script. I think I can achieve this by using

import sys 
inp_file = str(sys.argv[1])
exec("import %s" % inp_file)

inside script, but I would need to add the path where input.py is to the PYTHONPATH variable before running script. Is there a way to do it without having to do anything with environment variable?

nougako
  • 234
  • 1
  • 8
  • 1
    It's worth noting that `import input.py` wouldn't work; you'd need to do `import input` instead (though that would shadow the builtin `input` function), and if the module name were dynamic, you wouldn't be able to write any code using it; instead you might do `'from %s import *'` instead. Anyway, I've closed the question under an existing one with a better solution (i.e. `exec(open(inp_file).read())`). That'll put the variables in the global namespace, but if that's not what you wanted, you can [edit] to clarify and we can reopen the question. – wjandrea Jun 02 '23 at 00:20
  • BTW, the elements of `sys.argv` are `str`, so no need to call `str()` on them. – wjandrea Jun 02 '23 at 00:20
  • Another option you might consider is putting the parameters in a JSON file or something like that. – wjandrea Jun 02 '23 at 00:22
  • @wjandrea does your solution work if I run `script input.py` from within the directory where `input.py` is located (and `script` is not in this directory)? – nougako Jun 02 '23 at 00:24
  • If `script` is [runnable as a command](/q/27494758/4518341), it'll work. (Although BTW, there's already a [`script` command](https://en.wikipedia.org/wiki/Script_(Unix)) on Unix-like OS's.) – wjandrea Jun 02 '23 at 00:29
  • I tried it and it does run, thanks! But just to be sure, does `exec(open(inp_file).read())` effectively have the effect of copying and pasting the content of `inp_file` at the point where this command is invoked within `script`? I just want to be sure that I can use any python syntax inside `input.py`. BTW thanks for telling me about the overlap in file naming with an existing command. – nougako Jun 02 '23 at 00:44
  • 1
    Yes, exactly. `exec` basically means "take this code and run it as if it were written here". Np :) – wjandrea Jun 02 '23 at 00:53

0 Answers0