Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

While running the simple Python program below, I'm getting the following error:

./ line 2: syntax error near unexpected token `('                  
./ line 2: `response = urllib2.urlopen('')'

import urllib2      
response = urllib2.urlopen('')  
print "Response:", response

# Get the URL. This gets the real URL. 
print "The URL is: ", response.geturl()

# Getting the code
print "This gets the code: ", response.code

# Get the Headers. 
# This returns a dictionary-like object that describes the page fetched, 
# particularly the headers sent by the server
print "The Headers are: ",

# Get the date part of the header
print "The Date is: ",['date']

# Get the server part of the header
print "The Server is: ",['server']

# Get all data
html =
print "Get all data: ", html

# Get only the length
print "Get the length :", len(html)

# Showing that the file object is iterable
for line in response:
 print line.rstrip()

# Note that the rstrip strips the trailing newlines and carriage returns before
# printing the output.
share|improve this question
I just copied and pasted your script and it isn't giving me any errors! Are you sure about the syntax errors? Which version of python are you using? Also, I think this has nothing directly to do with ubuntu, has it? Chances are moderators might delete it! – Python Student Sep 23 '13 at 7:11
I'm using python 2.6.... – Naive Sep 23 '13 at 7:40
Just try double quotes around the url and see if anything happens. – Python Student Sep 23 '13 at 7:53
up vote 4 down vote accepted

You missed to add a shebang at the beginning of the script to tell the shell to interpret your script as a python script. Something like this:



#!/usr/bin/env python

See also: Why do people write #!/usr/bin/env python on the first line of a Python script?

share|improve this answer

Those errors are not typical Python tracebacks. Those errors look like shell errors.

I think you are trying to run a Python script with Bash (or an another shell), i.e. you are doing something like:


Instead, you should use:

share|improve this answer
now i got the o/p,thank you... – Naive Sep 23 '13 at 8:36

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.