./xx.py: line 1: import: command not found
Question: I am trying to use this [Python urllib2 Basic Auth
Problem](http://stackoverflow.com/questions/2407126/python-urllib2-basic-auth-
problem) bit of code to download a webpage content from an URL which requires
authentication. The code I am trying is:

    
    
     import urllib2, base64
    
    request = urllib2.Request("http://api.foursquare.com/v1/user")
    base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
    request.add_header("Authorization", "Basic %s" % base64string)   
    result = urllib2.urlopen(request)
    

It's showing me:

    
    
    ./xx.py: line 1: import: command not found
    ./xx.py: line 3: syntax error near unexpected token `('
    ./xx.py: line 3: `request = urllib2.Request("http://api.foursquare.com/v1/user")'
    

I am wondering what I am doing wrong? I am using `Python 2.7.5`. How can I
download file contents from an URL which requires authentication?


Answer: It's not an issue related to authentication at the first step. Your `import`
is not working. So, try writing this on first line:

    
    
    #!/usr/bin/python
    

and for the time being run using

    
    
    python xx.py
    

For you here is one explanation:

    
    
    >>> abc = "Hei Buddy"
    >>> print "%s" %abc
    Hei Buddy
    >>> 
    
    >>> print "%s" %xyz
    
    Traceback (most recent call last):
      File "<pyshell#6>", line 1, in <module>
        print "%s" %xyz
    NameError: name 'xyz' is not defined
    

At first, I initialized abc variable and it works fine. On the otherhand, xyz
doesn't work as it is not initialized!

