python urllib error - AttributeError: 'module' object has no attribute 'request' - urllib

I am trying out a tutorial code which fetches the html code form a website and prints it. I'm using python 3.4.0 on ubuntu. The code:
import urllib.request
page = urllib.request.urlopen("http://www.brainjar.com/java/host/test.html")
text = page.read().decode("utf8")
print(text)
I saw previous solutions and tried them, I also tried importing only urllib but it still doesn't work. The error message displayed is as shown:
Traceback (most recent call last):
File "string.py", line 1, in <module>
import urllib.request
File "/usr/lib/python3.4/urllib/request.py", line 88, in <module>
import http.client
File "/usr/lib/python3.4/http/client.py", line 69, in <module>
import email.parser
File "/usr/lib/python3.4/email/parser.py", line 12, in <module>
from email.feedparser import FeedParser, BytesFeedParser
File "/usr/lib/python3.4/email/feedparser.py", line 27, in <module>
from email import message
File "/usr/lib/python3.4/email/message.py", line 15, in <module>
from email import utils
File "/usr/lib/python3.4/email/utils.py", line 40, in <module>
from email.charset import Charset
File "/usr/lib/python3.4/email/charset.py", line 15, in <module>
import email.quoprimime
File "/usr/lib/python3.4/email/quoprimime.py", line 44, in <module>
from string import ascii_letters, digits, hexdigits
File "/media/saiwal/D89602199601F930/Documents/Copy/codes/python/headfirst/string.py", line 2, in <module>
page = urllib.request.urlopen("http://www.brainjar.com/java/host/test.html")
AttributeError: 'module' object has no attribute 'request'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 21, in <module>
from urllib.request import urlopen
File "/usr/lib/python3.4/urllib/request.py", line 88, in <module>
import http.client
File "/usr/lib/python3.4/http/client.py", line 69, in <module>
import email.parser
File "/usr/lib/python3.4/email/parser.py", line 12, in <module>
from email.feedparser import FeedParser, BytesFeedParser
File "/usr/lib/python3.4/email/feedparser.py", line 27, in <module>
from email import message
File "/usr/lib/python3.4/email/message.py", line 15, in <module>
from email import utils
File "/usr/lib/python3.4/email/utils.py", line 40, in <module>
from email.charset import Charset
File "/usr/lib/python3.4/email/charset.py", line 15, in <module>
import email.quoprimime
File "/usr/lib/python3.4/email/quoprimime.py", line 44, in <module>
from string import ascii_letters, digits, hexdigits
File "/media/saiwal/D89602199601F930/Documents/Copy/codes/python/headfirst/string.py", line 2, in <module>
page = urllib.request.urlopen("http://www.brainjar.com/java/host/test.html")
AttributeError: 'module' object has no attribute 'request'
Original exception was:
Traceback (most recent call last):
File "string.py", line 1, in <module>
import urllib.request
File "/usr/lib/python3.4/urllib/request.py", line 88, in <module>
import http.client
File "/usr/lib/python3.4/http/client.py", line 69, in <module>
import email.parser
File "/usr/lib/python3.4/email/parser.py", line 12, in <module>
from email.feedparser import FeedParser, BytesFeedParser
File "/usr/lib/python3.4/email/feedparser.py", line 27, in <module>
from email import message
File "/usr/lib/python3.4/email/message.py", line 15, in <module>
from email import utils
File "/usr/lib/python3.4/email/utils.py", line 40, in <module>
from email.charset import Charset
File "/usr/lib/python3.4/email/charset.py", line 15, in <module>
import email.quoprimime
File "/usr/lib/python3.4/email/quoprimime.py", line 44, in <module>
from string import ascii_letters, digits, hexdigits
File "/media/saiwal/D89602199601F930/Documents/Copy/codes/python/headfirst/string.py", line 2, in <module>
page = urllib.request.urlopen("http://www.brainjar.com/java/host/test.html")
AttributeError: 'module' object has no attribute 'request'

This looks like a nasty coincidence.
TL;DR: Don’t name your script string.py.
So what’s happening here?
You’re trying to import urllib.request.
urllib.request tries to import http.client, which tries to import email.parser, which tries to import email.feedparser, which tries to import email.message, which tries to import email.utils, which tries to import email.charset, which tries to import email.quoprimime.
email.quoprimime tries to import string, expecting it to be the standard Python string module—but since the current working directory has priority over the standard Python library directories, it finds your string.py instead and tries to import that.
When importing your string.py, you try to import urllib.request. Since urllib.request is still being imported, you get back a skeleton urllib without a request attribute yet.
Because your imported string.py then fails because it can’t find the request attribute, the exception starts propagating back up.
But wait, there’s more! Since there was an error during an import, Ubuntu tries to be helpful by seeing if you’re missing a dpkg package. If so, it could say “hey, it looks like you’re missing this module; want to apt-get it?” So the mechanism for looking up the appropriate package is activated…
…but the module for looking up the appropriate package itself depends on urllib.request, so it tries to import it, and again fails…
In short, because you picked string.py as a file name, you overrode the standard string module, which broke a lot of other modules, and even broke the module that was supposed to be helpful when you were missing a module, causing a whole lot of havoc. Fortunately the solution is easy: rename your script.

Related

Error when installing Pandas on Solaris 10

Currently we are on Python 2.7.17, and we are getting the following error:
sbdsvrwm261# ./pip install pandas
Traceback (most recent call last):
File "./pip", line 9, in <module>
load_entry_point('pip==8.1.2', 'console_scripts', 'pip')()
File "/opt/csw/lib/python2.7/site-packages/pkg_resources/__init__.py", line 565, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/opt/csw/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2631, in load_entry_point
return ep.load()
File "/opt/csw/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2291, in load
return self.resolve()
File "/opt/csw/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2297, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/opt/csw/lib/python2.7/site-packages/pip/__init__.py", line 16, in <module>
from pip.vcs import git, mercurial, subversion, bazaar # noqa
File "/opt/csw/lib/python2.7/site-packages/pip/vcs/subversion.py", line 9, in <module>
from pip.index import Link
File "/opt/csw/lib/python2.7/site-packages/pip/index.py", line 30, in <module>
from pip.wheel import Wheel, wheel_ext
File "/opt/csw/lib/python2.7/site-packages/pip/wheel.py", line 39, in <module>
from pip._vendor.distlib.scripts import ScriptMaker
File "/opt/csw/lib/python2.7/site-packages/pip/_vendor/distlib/scripts.py", line 14, in <module>
from .compat import sysconfig, detect_encoding, ZipFile
File "/opt/csw/lib/python2.7/site-packages/pip/_vendor/distlib/compat.py", line 31, in <module>
from urllib2 import (Request, urlopen, URLError, HTTPError,
ImportError: cannot import name HTTPSHandler
If you require further information from me, please let me know.
Thanks
Pandas officially supports Python ^3.5.3.
Have you tried using virtualenv or docker to get Python 3.*?

Receive error when I run an executable created after py2exe conversion

After running my setup.py file and attempt to run the newly created .exe file I receive an error log that says:
Traceback (most recent call last):
File "Healthy_temp.py", line 7, in <module>
File "pandas\__init__.pyc", line 42, in <module>
File "pandas\core\api.pyc", line 26, in <module>
File "pandas\core\groupby\__init__.pyc", line 1, in <module>
File "pandas\core\groupby\groupby.pyc", line 37, in <module>
File "pandas\core\frame.pyc", line 100, in <module>
File "pandas\core\series.pyc", line 4390, in <module>
File "pandas\core\generic.pyc", line 10138, in _add_series_or_dataframe_operations
File "pandas\core\window.pyc", line 14, in <module>
File "pandas\_libs\window.pyc", line 12, in <module>
File "pandas\_libs\window.pyc", line 10, in __load
File "pandas\_libs\skiplist.pxd", line 31, in initpandas._libs.window
ImportError: No module named skiplist
My setup.py file is here:
from distutils.core import setup
import py2exe
import matplotlib
from PyQt4 import QtCore, QtGui
from AboutDialog import Ui_Dialog
from matplotlibwidget import MatplotlibWidget
import sys
from os import walk
from os import getcwd
import pandas
import numpy
from glob import glob
from datetime import datetime
from math import isnan
import sip
# setup(windows=['Healthy_temp.py'])
setup(options={'py2exe': {'bundle_files': 3, 'compressed': True}}, windows=[
'Healthy_temp.py'], zipfile=None,
data_files=matplotlib.get_py2exe_datafiles())
What can I do to correct this problem?
Add 'pandas._libs.skiplist' to the "includes" option list.

import tensorflow.contrib.layers as layers reported an error

The TF version is 1.9.0 and cython version is 0.28.4, when import tensorflow.contrib.layers as layers, there is an error occured,
Traceback (most recent call last):
File "train.py", line 9, in <module>
import tensorflow.contrib.layers as layers
File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/__init__.py", line 39, in <module>
from tensorflow.contrib import distributions
File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/distributions/__init__.py", line 40, in <module>
from tensorflow.contrib.distributions.python.ops.estimator import *
File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/distributions/python/ops/estimator.py", line 21, in <module>
from tensorflow.contrib.learn.python.learn.estimators.head import _compute_weighted_loss
File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/__init__.py", line 95, in <module>
from tensorflow.contrib.learn.python.learn import *
File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/__init__.py", line 28, in <module>
from tensorflow.contrib.learn.python.learn import *
File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/__init__.py", line 30, in <module>
from tensorflow.contrib.learn.python.learn import estimators
File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/__init__.py", line 302, in <module>
from tensorflow.contrib.learn.python.learn.estimators.dnn import DNNClassifier
File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/dnn.py", line 35, in <module>
from tensorflow.contrib.learn.python.learn.estimators import dnn_linear_combined
File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/dnn_linear_combined.py", line 36, in <module>
from tensorflow.contrib.learn.python.learn.estimators import estimator
File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 52, in <module>
from tensorflow.contrib.learn.python.learn.learn_io import data_feeder
File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/learn_io/__init__.py", line 26, in <module>
from tensorflow.contrib.learn.python.learn.learn_io.dask_io import extract_dask_data
File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/learn_io/dask_io.py", line 33, in <module>
import dask.dataframe as dd
File "/usr/local/lib/python3.5/dist-packages/dask/dataframe/__init__.py", line 3, in <module>
from .core import (DataFrame, Series, Index, _Frame, map_partitions,
File "/usr/local/lib/python3.5/dist-packages/dask/dataframe/core.py", line 20, in <module>
from .. import array as da
File "/usr/local/lib/python3.5/dist-packages/dask/array/__init__.py", line 4, in <module>
from .core import (Array, block, concatenate, stack, from_array, store,
File "/usr/local/lib/python3.5/dist-packages/dask/array/core.py", line 50, in <module>
from ..bytes.core import get_mapper, get_fs_token_paths
File "/usr/local/lib/python3.5/dist-packages/dask/bytes/__init__.py", line 4, in <module>
from .core import read_bytes, open_files, open_text_files
File "/usr/local/lib/python3.5/dist-packages/dask/bytes/core.py", line 10, in <module>
from .compression import seekable_files, files as compress_files
File "/usr/local/lib/python3.5/dist-packages/dask/bytes/compression.py", line 31, in <module>
import snappy
File "/usr/local/lib/python3.5/dist-packages/snappy/__init__.py", line 7, in <module>
from .SnapPy import (AbelianGroup, HolonomyGroup, FundamentalGroup,
File "cython/core/basic.pyx", line 45, in init SnapPy
File "/usr/local/lib/python3.5/dist-packages/snappy/horoviewer.py", line 3, in <module>
from .CyOpenGL import *
File "opengl/CyOpenGL.pyx", line 36, in init CyOpenGL
AttributeError: type object 'CyOpenGL.vector3' has no attribute '__reduce_cython__'
I thought there is something wrong with Cython's verion, but I am not sure since. How can I fix it?
Now, I can not figure out how did this error come out, but still, there is a workaround that is using tf.layers.dense to replace it
tf.layers.dense(...) # tf.contrib.layers.fully_connected(...)
This error is nasty when you don't have root permission to update dependencies.
Reference:
Are tf.layers.dense() and tf.contrib.layers.fully_connected() interchangeable?

ImportError: cannot import name ResponseError

I am trying to use mod_python.
however when i try to access my page I get following error.
Traceback (most recent call last):
File "/usr/lib64/python2.6/site-packages/mod_python/importer.py", line 1540, in HandlerDispatch
default=default_handler, arg=req, silent=hlist.silent)
File "/usr/lib64/python2.6/site-packages/mod_python/importer.py", line 1205, in _process_target
module = import_module(module_name, path=path)
File "/usr/lib64/python2.6/site-packages/mod_python/importer.py", line 299, in import_module
log, import_path)
File "/usr/lib64/python2.6/site-packages/mod_python/importer.py", line 683, in import_module
execfile(file, module.__dict__)
File "/var/www/pylons-data/prod/reports/scripts/access_reports.py", line 2, in <module>
import requests
File "/usr/lib/python2.6/site-packages/requests/__init__.py", line 60, in <module>
from .api import request, get, head, post, patch, put, delete, options
File "/usr/lib/python2.6/site-packages/requests/api.py", line 14, in <module>
from . import sessions
File "/usr/lib/python2.6/site-packages/requests/sessions.py", line 27, in <module>
from .adapters import HTTPAdapter
File "/usr/lib/python2.6/site-packages/requests/adapters.py", line 29, in <module>
from .packages.urllib3.exceptions import ResponseError
ImportError: cannot import name ResponseError
Any help is appreciated. not sure how i can fix
Maybe Make sure that yours module is in sys.path list

cannot import matplotlib.pylab with matplotlib installed

I understand that there have been several questions asked on this topic, however, I didn't find them able to solve my problem.
I installed matplotlib on the maching under /python-path/site-packages, both library and python are 64bits,
so I could import matplotlib no problem, but when I tried to import matplotlib.pylab or pylab, it gives me the following error
Traceback (most recent call last):
File "shownet.py", line 40, in <module>
import matplotlib.pylab
File "/usr/lib64/python2.6/site-packages/matplotlib/pylab.py", line 247, in <module>
from matplotlib.pyplot import *
File "/usr/lib64/python2.6/site-packages/matplotlib/pyplot.py", line 78, in <module>
new_figure_manager, draw_if_interactive, show = pylab_setup()
File "/usr/lib64/python2.6/site-packages/matplotlib/backends/__init__.py", line 25, in pylab_setup
globals(),locals(),[backend_name])
File "/usr/lib64/python2.6/site-packages/matplotlib/backends/backend_gtkagg.py", line 10, in <module>
from matplotlib.backends.backend_gtk import gtk, FigureManagerGTK, FigureCanvasGTK,\
File "/usr/lib64/python2.6/site-packages/matplotlib/backends/backend_gtk.py", line 8, in <module>
import gtk; gdk = gtk.gdk
File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py", line 64, in <module>
_init()
File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py", line 52, in _init
_gtk.init_check()
Would anyone know why this is so?
Also this is on linux, and I built it using setup.py