Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call a specific python function in a file from the command line? [duplicate]

Tags:

python

Say there is the following function in a file called fb_auth_token.py

def get_fb_access_token(email, password):
...
return ...

How would I run this from bash? python fb_auth_token.py get_fb_access?. How do I call just the one specific function?

like image 429
Martin Erlic Avatar asked Sep 18 '26 19:09

Martin Erlic


2 Answers

You could either call python with the command option:

python -c "from file_name import function;function()"

or if you want this function to be called every time you execute the script you could add the line

if __name__ == "__main__":
    function()

This will then only execute this function if the file is executed directly (i.e. you can still import it into another script without "function" being called).

like image 113
Simon Hobbs Avatar answered Sep 21 '26 10:09

Simon Hobbs


I figured it out:

python -c 'import fb_auth_token; print fb_auth_token.get_fb_access_token("email", "password")'

like image 24
Martin Erlic Avatar answered Sep 21 '26 11:09

Martin Erlic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!