I have an "alarm email" function inside a python module. I want to be able to call this function from a bash script. I know you can call a module using 'python ' in the script, but I'm not if you can or how you would call a specific function within the module.
Asked
Active
Viewed 5.5k times
4 Answers
66
python -c'import themodule; themodule.thefunction("boo!")'

Alex Martelli
- 854,459
- 170
- 1,222
- 1,395
-
1what if you wanted to pass in a string to the python function? or to simply print hello? `python -c 'print("hello")'` doesn't work. – mango Jul 02 '14 at 18:52
-
1@mango - Incorrect - arguments DO work. I copied and pasted your example, in fact, and then tested again using both 'python3' and 'python2'. You may have a different problem. – Scott Prive Feb 06 '15 at 18:18
-
What if the python module is in another directory? – Kias Aug 21 '15 at 03:38
-
8if the module is in a different directory.`python -c 'import sys;sys.path.insert(0,"/var/lib/path");import module as m;m.function()'` – Iceman May 07 '18 at 22:13
23
You can use the -c
option:
python -c "import random; print random.uniform(0, 1)"
Modify as you need.

carl
- 49,756
- 17
- 74
- 82
3
To call a specific function in a module, assure that the module has the following:
if __name__ == "__main__":
the_function_to_call( )
Then you can simply do this in your shell script.
python module.py
Or
python -m module
Depending on whether or not the module's on the PYTHONPATH
.

S.Lott
- 384,516
- 81
- 508
- 779
-
This would only work if you didn't already have a main or already had a main that pointed to the function you need. In my case there is already a main and it points to another function in the module so this wasn't an option. – Daniel Jan 22 '10 at 21:32
-
@Daniel: That's not a limitation, that's how your design things. If you've already got a `main` and it does the wrong thing, you've designed it wrong. – S.Lott Jan 24 '10 at 13:42
-
2@S.Lott: It's not designed wrong. The method by itself does exactly what it needs to do. It is a limitation because its a module not a class which means that it is a grouping of like functions. The main points at the one that is typically called, but in this case I need to start with a non-typical function thus the direct call. – Daniel Jan 25 '10 at 16:40
-
2@Daniel: Two choices? That's what command-line parsing is all about. If you cannot simply run this from the command line, you did not design it correctly to be run from the command line. You have `optparse`, which will handle this more neatly than what you're trying. – S.Lott Jan 25 '10 at 17:17
-
2@S.Lott: Good call on optparse. It honestly never crossed my mind to use that. I'm happy with the solution above for now, but optparse will definitely be something to look at in the future. – Daniel Feb 01 '10 at 22:24
-
simple and straightforward. solved my problem. for the interested ones, I was calling the "the_function_to_call" function without the validation, which was duplicating the requests when calling from different scripts. – Bevilaqua Jun 19 '18 at 01:14
1
To supplements others' answers with two notes:
- If you need to feed environment variables as parameters, use double quotes;
- The parameters are not necessarily str.
Here is an example:
# test.py
def call(param):
print(f'{param} with type={type(param)}')
print('hi')
Execute from shell:
PARAM=10
python3 -c "import test; test.call(${PARAM})"
You can read:
hi
10 with type=<class 'int'>

Tengerye
- 1,796
- 1
- 23
- 46