diff --git a/spacy/compat.py b/spacy/compat.py index dc0883542..5359edefc 100644 --- a/spacy/compat.py +++ b/spacy/compat.py @@ -33,11 +33,23 @@ try: except ImportError: from thinc.neural.optimizers import Adam as Optimizer +try: + import urllib +except ImportError: + import urllib2 as urllib + +try: + from urllib.error import HTTPError as url_error +except ImportError: + from urllib2 import HTTPError as url_error + pickle = pickle copy_reg = copy_reg CudaStream = CudaStream cupy = cupy copy_array = copy_array +urllib = urllib +url_error = url_error izip = getattr(itertools, 'izip', zip) is_windows = sys.platform.startswith('win') @@ -56,6 +68,7 @@ if is_python2: input_ = raw_input # noqa: F821 json_dumps = lambda data: ujson.dumps(data, indent=2, escape_forward_slashes=False).decode('utf8') path2str = lambda path: str(path).decode('utf8') + url_open = lambda url: urllib.urlopen(url) elif is_python3: bytes_ = bytes @@ -64,6 +77,7 @@ elif is_python3: input_ = input json_dumps = lambda data: ujson.dumps(data, indent=2, escape_forward_slashes=False) path2str = lambda path: str(path) + url_open = lambda url: urllib.request.urlopen(url) def b_to_str(b_str):