Question

I am trying to fetch webpages using urlib. I am using Python 3.3 and Sublime Text 2 i am facing a problem when i execute the following code

import os
from urllib.request import urlopen
remoteaddr = 'http://paulgraham.com/fr.html'
remotefile = urlopen(remoteaddr)
remotedata = remotefile.readlines()
remotefile.close()
for line in remotedata:
    print(line)

it produces the following error

Traceback (most recent call last):
  File "..\Documents\Pydev\ftp\test.py", line 2, in <module>
    from urllib.request import urlopen
ImportError: bad magic number in 'urllib': b'\x03\xf3\r\n'
[Finished in 0.1s with exit code 1]

if i run the code with Aptana it executes properly, i would prefer to use ST2 for my dev, any ideas on what the problem might be? I have added ST2 to allowed programs in the firewall.

Was it helpful?

Solution

It seems your urllib.py is compiled for wrong python version. Putting away how it was achieved, as urrlib is normal python file, try removing urllib.pyc from Python/lib folder, and see if it helps.

UPDATE

Putting away was wrong. It got to my mind that Sublime Text 2, althrough entirely not used in your snippet, is working under Python 2 (in fact python 2.6), and its windows package contains compiled urllib within. So it is pretty evident that urllib from Sublime is being imported.

So right course of actions will be migrate to Sublime Text 3 (beta). Other option might've been downgrading to python 2.6, but judging from python and windows versions, it seems you'll choose to upgrade.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top