Skip to content

Instantly share code, notes, and snippets.

@sbliven
Created April 22, 2021 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbliven/d413c2f2f4af3bf56e13d1f8656751b1 to your computer and use it in GitHub Desktop.
Save sbliven/d413c2f2f4af3bf56e13d1f8656751b1 to your computer and use it in GitHub Desktop.
Example of reading a "settings" python module
"""A settings module based on global variables"""
name = "Custom Settings"
x = 4
y = 0
"""main module
usage: python read_settings_py.py mysettings
This will import settings from mysettings.py
"""
import argparse
import importlib
def main(args=None):
parser = argparse.ArgumentParser(description="")
parser.add_argument("settings", help="settings file")
args = parser.parse_args(args)
# Dynamic import based on the command line argument
settings = importlib.import_module(args.settings)
print("Name: %s" % settings.name)
print("Coords: %d,%d" % (settings.x, settings.y))
if __name__ == "__main__":
main()
#!/bin/bash
# Slurm submission script
# usage: sbatch submit_py.sh mysettings
#SBATCH -p hourly
#SBATCH -n 1
#SBATCH --account=merlin
module purge
module load anaconda
conda activate dev27
# pass sbatch arguments to python
python read_settings_py.py "$@"
@sbliven
Copy link
Author

sbliven commented Apr 22, 2021

Here is an example of submitting a slurm job with an argument. It uses a bare module to store settings, similar to the current implementation.

  • Call sbatch submit_py.sh mysettings
  • Inside submit_py.sh, mysettings is stored as $@ (standard bash argument handling)
  • On the compute node, submit_py.sh calls python read_settings_py.py mysettings
  • read_settings_py.py gets "mysettings" passed as sys.argv[1]. This is parsed by the cli library (here the built-in argparse, but click is nicer) and stored as args.settings
  • The module gets dynamically imported with importlib.import_module. This line would be equivalent to import mysettings. It would search sys.path and find ./mysettings.py

@sbliven
Copy link
Author

sbliven commented Apr 22, 2021

Using python modules for storing settings is not recommended. The official way of storing settings is in ini files. Here's a better implementation: https://gist.github.com/sbliven/10c3ddabcb32301b9fb475748ed75e35

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment