urllib.request in Python 2.7 - python

I can use urllib.request module with Python 3.1. But when I execute the same program using Python 2.7, an error comes along the lines of;
AttributeError: 'module' object has no attribute 'request'.
I believe this error is because theres no request module in urllib for Python 2.7. Because I need to use tweepy i'll have to stick with Python 2.7 since tweepy does not support Python 3.
So how I can use urllib.request module in Python 2.7?

It is also possible to use six module to make code for both python2 & python3:
from six.moves import urllib
# ...
result = urllib.request.urlopen(url)

Use urllib2.urlopen for Python 2.

Take a look at http://docs.python.org/library/urllib2.html.
The urllib2 module is the predecessor to urllib.request/urllib.error (it has been split into those modules in Python 3.0).

Related

Import library zip in Python

I am looking at an older version of python code
from itertools import izip
but when I run it, it says
ImportError: cannot import name 'izip'
Anyone know how should I import?
In Python 3+ you just use zip instead. It's builtin, so you don't have to import it. Try using the 2to3 tool to update the code automatically.

urllib vs. urllib.request in Python3 - Enthought Canopy

Getting strange difference inside Enthought Canopy vs. command line when trying to load and utilize urllib and/or urllib.request
Here's what I mean. I'm running Python 3.5 on MacOS 10.11.3. But I've tried this on Windows 10 machine too, and I'm getting the same results. The difference appears to be between using Canopy and using command line.
I'm trying to do basic screen scraping. Based on reading, I think I should be doing:
from urllib.request import urlopen
html = urlopen("http://pythonscraping.com/pages/page1.html")
print(html.read())
This works at a command prompt.
BUT, inside canopy, this does not work. Inside canopy I get the error
ImportError: No module named request
When Canopy tries to execute the from urllib.request import urlopen
Inside Canopy, THIS is what works:
import urllib
html = urllib.urlopen("http://pythonscraping.com/pages/page1.html")
print(html.read())
I would really like to understand what is happening, as I don't want my Canopy python scripts to fail when I run them outside of Canopy. Also, the Canopy approach does not seem consistent with docs that I've read... I just got there by trial & error.
urllib.request is a module that only exists in Python 3. Enthought Canopy Distribution still ships with a version of Python 2.7 (2.7.10 as of the current version 1.6.2).
In Python 2.x, you have the choice of using either urllib or urllib2, which expose functions like urlopen at the top level (e.g. urllib.urlopen rather than urllib.request.urlopen).
If you want your scripts to be able to run through either Python 3.x or in Enthought Canopy's Python distribution, then there are two possible solutions:
Use requests - this is generally the recommended library to use for interacting with HTTP in Python. It's a third-party module which you can install using standard pip or easy_install, or from the Canopy Package Index.
Your equivalent code would look similar to:
# This allows you to use the print() function inside Python 2.x
from __future__ import print_function
import requests
response = requests.get("http://pythonscraping.com/pages/page1.html")
print(response.text)
Use conditional importing to bring in the current function you need regardless of version. This is just using built-in features of Python and will not require third-party libraries.
Your code would then look similar to:
# This allows you to use the print() function inside Python 2.x
from __future__ import print_function
import sys
try:
# Try importing Python 3's urllib.request first.
from urllib.request import urlopen
except ImportError:
# Looks like we're running Python 2.something.
from urllib import urlopen
response = urlopen("http://pythonscraping.com/pages/page1.html")
# urllib.urlopen's response object is different based
# on Python version.
if sys.version_info[0] < 3:
print(response.read())
else:
# Python 3's urllib responses return the
# stream as a byte-stream, and it's up to you
# to properly set the encoding of the stream. This
# block just checks if the stream has a content-type set
# and if not, it defaults to just using utf-8
encoding = response.headers.get_content_charset()
if not encoding:
encoding = 'utf-8'
print(response.read().decode(encoding))

convert python 3.x code to 2.x under python 2.7.9

I only have python 2.7.9 installed on my PC. I need to use a code written in 3.x. So I want to convert it to 2.x version syntax before executing it.
I saw "3to2" package, for example:
https://pypi.python.org/pypi/3to2/1.0
However, it is said "This branch of 3to2 must be run with Python 3." in its "README" file.
So I wonder if it is possible to convert 3.x code to 2.x under python 2.7.9?
More specifically, I have from html.parser import HTMLParser in the script, which is only available in version 3. If I should use from __future__ import, how exactly should I do?
Just change from html.parser import HTMLParser to from HTMLParser import HTMLParser.

ENUM module and cStringIO module in PYVISA

I have some trouble to fix. I am using Python 3.2 with pyvisa for Python 3.2 32bits. When i used:
import pyvisa
It displayed:
ImportError: No module named enum
But when I use:
import pyqtgraph, pyvisa
I get:
ImportError: No module named cStringIO
I just want to use pyvisa for using an Agilent 33250a by GPIB.
The enum module wasn't part of Python until Python 3.4, so 3.2 is too early; you need to upgrade, or you need to live without enum (upgrading is a good idea mind you; the performance and features of Python have improved markedly since then; on performance in particular, strings and user defined class instances dramatically reduced their memory overhead). I'm guessing pyvisa dropped support for Python versions older than 3.4 if they're depending on enum.
cStringIO is a Python 2.x only accelerator module for StringIO; in Python 3.0 and higher, you just import io and use io.StringIO, and it will automatically use the C accelerated code under the hood when available, and pure Python code otherwise. If you're only targeting Python 3, just do import io or from io import StringIO. For code that should run under both Python 2 and Python 3, and use str in both, you can do the following for imports:
try:
from cStringIO import StringIO # Py2 C accelerated version
except ImportError:
try:
from StringIO import StringIO # Py2 fallback version
except ImportError:
from io import StringIO # Py3 version
If you want to handle Unicode text regardless of Python version (well, in 2.6 and up), you can just use io.StringIO exclusively; it works with unicode in Py2, and str in Py3, which means it handles all text in both versions (where cStringIO only handles str in Py2, so it can't handle the whole Unicode range).
I suspect your other import error for pyqtgraph would be because you tried installing a version of pyqtgraph written for Python 2; the pyqtgraph page claims Python 3.x compatibility, and use of cStringIO without a fallback would not meet that claim, so either you installed the wrong version, or it was installed incorrectly (e.g. if they were using a single code base and 2to3-ing it, but you somehow installed it without 2to3-ing it; no idea how you'd do that).

Urllb2 package missing from Pypi

I wanted to install urllib2 package from PyPI but it is not available.
It seems that it has been updated to urllib3, but is there any way to download urllib2 ?
import urllib2
Is that what you want?
If you find any library under http://docs.python.org/ you can always import without installing it.
Update 1:
If you need the source code...
The official Cpython code: http://hg.python.org/cpython/file/3b5fdb5bc597/Lib/urllib
Note The urllib2 module has been split across several modules in
Python 3 named urllib.request and urllib.error. The 2to3 tool will
automatically adapt imports when converting your sources to Python 3.
or try this? http://code.reddit.com/docs/urllib2-pysrc.html
I can't guarantee the integrity for the second alternative link.

Categories