How to get around urllib2.HTTP when running a python script

Multi tool use
How to get around urllib2.HTTP when running a python script
I'm trying to run a python script in Windows 7 in the command prompt, however I keep receiving this error every time I try to run it.
File "inb4404.py", line 73
except urllib2.HTTPError, err:
^
Syntax Error: invalid syntax
I'm not sure what I'm doing wrong. I located the directory containing the python script using 'cd' and also have Python in my PATH. Then I wrote 'python inb4404.py' and received that error.
Any pointers would be greatly appreciated, thank you.
Thank you so much. That fixed the problem.
– Justin Song
Jul 2 at 1:16
2 Answers
2
The script you are using is written for Python 2 but you are running it using Python 3.
Depending on what your goals are, you have a few options. The simplest would be to run the script using Python 2. You may already have it in the c:Python27python
directory or can install it. See these Window tips for details or download it here.
c:Python27python
If you want to get more involved, you can manually change the script over to Python 3. This would take some reading up on the differences such as the one you encountered with the except urllib2.HTTPError, err
becoming except urllib2.HTTPError as err
. The script you are running is small so it may be doable but if it relies on Python 2 only libraries it could get more complex fast. Additional tools and info for converting Python 2 to 3 is available at 2to3 which provides a tool that identifies issues and can even automatically fix them for you but like most automatic tools, it may not do everything you need.
urllib2.HTTPError, err
except urllib2.HTTPError as err
Your syntax is wrong. See https://docs.python.org/3/tutorial/errors.html.
If you want err
to be the error object, it should be except urllib2.HTTPError as err:
err
except urllib2.HTTPError as err:
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You're trying to run a Python 2 script in Python 3. Don't do that.
– jwodder
Jul 2 at 0:57