0

How can I call a function from a python script (Ubuntu shell) but also pass a config parameter at the same time? A relevant SO post does not seem to address that.

This is what I have for now:

$ python -c ' from python_library import * ; function() ; -config /path/to/config/file '

The above fails. And so do the following (as many other) combinations:

    $ python -c ' from python_library import * ; function() -config /path/to/config/file '

or

    $ python -c ' from python_library import * ; function() ; -config "/path/to/config/file" '

Thanks!

pebox11
  • 3,377
  • 5
  • 32
  • 57
  • ` -config "/path/to/config/file"` is not python code, why are you including it in the quoted command – avigil Jul 12 '18 at 16:04
  • I am trying to pass a parameter (a config file) to my script, and the examples included in the original post are failed attempts to do that. – pebox11 Jul 12 '18 at 16:06

2 Answers2

1

You need to pull the conf arg out as another argument to python

$ python -c ' from python_library import * ; function()' -config /path/to/config/file
avigil
  • 2,218
  • 11
  • 18
  • it worked (without the lone '-', that was included in your original post), thank you! – pebox11 Jul 12 '18 at 16:12
  • If you would like to add `&` at the end in order your command to run at the background and free up your shell prompt to issue another command? – pebox11 Jul 14 '18 at 20:59
1

You could use environment variable for that:

MYPARAMETERS="-config /path/to/config/file" python -c "import os,sys;sys.argv = os.environ['MYPARAMETERS'].split(); import python_library import * ; function()"
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91