2

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?

Martin Erlic
  • 5,467
  • 22
  • 81
  • 153
  • It depends on what exactly you want to do. You could for example just call it in your Python file: `print(get_fb_access_token(email, password))`. And then run `python fb_auth_token.py` – Feodoran Aug 15 '17 at 17:48

3 Answers3

9

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).

Simon Hobbs
  • 990
  • 7
  • 11
1

I figured it out:

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

Martin Erlic
  • 5,467
  • 22
  • 81
  • 153
1

You can use the -c command line argument. For example:

python -c 'import fb_auth_token; fb_auth_token.get_fb_access_token("email", "password")'
digitaLink
  • 458
  • 3
  • 17