Search code examples
pythonpython-2.7zapier

How do I run a Python script in Unix


I'm not a Python developer but I need to use this language to set up certain application. I'd like to do basic auth. Python documentation reads:

Making requests with HTTP Basic Auth is very simple:

>>> from requests.auth import HTTPBasicAuth
>>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass')) <Response [200]>

So I created a file where I put:

from requests.auth import HTTPBasicAuth
requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))

However, it is not as simple as the documentation claims. When I run the code I get the following error:

./auth.py: line 1: from: command not found
./auth.py: line 2: syntax error near unexpected token `'https://api.github.com/user','
./auth.py: line 2: `requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))'

I use Python 2.7.


Solution

  • Those are error messages from a bash script... you probably don't have the correct shebang at the top of your script:

    #!/usr/bin/env python
    

    Then run the script as ./my_script

    Or, use:

    python my_script.py
    

    To run the script.