I am trying to call urllib2 simple code.
import urllib2
f = urllib2.urlopen('http://www.python.org/')
print f.read(100)
m getting this error
File "urllib.py", line 13, in <module>
import urllib2
File "/usr/lib/python2.7/urllib2.py", line 111, in <module>
from urllib import (unwrap, unquote, splittype, splithost, quote,
File "/home/vclub/vclubdev/vclubcms/urllib.py", line 14, in <module>
f = urllib2.urlopen('http://www.python.org/')
AttributeError: 'module' object has no attribute 'urlopen'
Please suggest what i need to do for solving this error .
You have a file called urllib.py in your working directory. It's confusing Python's import mechanism.
You should rename it.
Related
I am trying to use urllib for the simple request as in below:
import urllib
my_url = "https://realpython.com/practice/aphrodite.html"
my_open = urllib.request.urlopen(my_url)
my_op = my_open.read().decode('utf-8')
print(my_op)
When I run code it returns me request from my database, which is not related to the urllib. Moreover, I got an error as in below:
import urllib.request
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 88, in <module>
import http.client
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 71, in <module>
import email.parser
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/email/parser.py", line 12, in <module>
from email.feedparser import FeedParser, BytesFeedParser
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/email/feedparser.py", line 27, in <module>
from email import message
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/email/message.py", line 16, in <module>
from email import utils
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/email/utils.py", line 29, in <module>
import socket
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socket.py", line 52, in <module>
import os, sys, io, selectors
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/selectors.py", line 290, in <module>
class SelectSelector(_BaseSelectorImpl):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/selectors.py", line 317, in SelectSelector
_select = select.select
AttributeError: module 'select' has no attribute 'select'
I can uninstall and install any library except urllib. Please guide me about this error.
I created a separate file with the name of select.py and didn't include any code to that. After that, I didn't get any error.
I'm a Mac user and I'm trying to use the requests module get() here:
import requests
url = 'http://www.omdbapi.com/?t=star+wars&r=json'
response = requests.get(url)
dic = response.json()
for key in dic:
print key, ':', dic[key]
But I get this:
import requests
File "/Library/Python/2.7/site-packages/requests/__init__.py", line 63, in <module>
from . import utils
File "/Library/Python/2.7/site-packages/requests/utils.py", line 24, in <module>
from ._internal_utils import to_native_string
File "/Library/Python/2.7/site-packages/requests/_internal_utils.py", line 11, in <module>
from .compat import is_py2, builtin_str, str
File "/Library/Python/2.7/site-packages/requests/compat.py", line 33, in <module>
import json
File "/Users/miguel/Desktop/json.py", line 4, in <module>
response = requests.get(url)
AttributeError: 'module' object has no attribute 'get'
I've reinstalled the requests library using pip module but it doesn't fix.
Any idea? Thanks.
You've named your own script json.py which we can see in the last line of the traceback:
File "/Users/miguel/Desktop/json.py", line 4, in <module>
Don't do that, you're now masking the actual json module and that's going to do some damage to requests's ability to function. Equally, don't call a script requests.py. Just rename the file and it should fix the issue.
I am new to Python 3.4.5 which I am learning online by watching videos with some good knowledge of C. I am trying to download an image via Python which I am unable to do because of this error.
Code:
import random
import urllib.request
def img(url):
full='name'+'.jpeg'
urllib.request.urlretrieve(url,full)
img("http://lorempixel.com/400/200")
Error:
Traceback (most recent call last):
File "image.py", line 2, in <module>
import urllib.request
File "/home/yuvi/pyth/urllib/request.py", line 88, in <module>
import http.client
File "/usr/local/lib/python3.4/http/client.py", line 69, in <module>
import email.parser
File "/usr/local/lib/python3.4/email/parser.py", line 12, in <module>
from email.feedparser import FeedParser, BytesFeedParser
File "/usr/local/lib/python3.4/email/feedparser.py", line 27, in <module>
from email import message
File "/usr/local/lib/python3.4/email/message.py", line 16, in <module>
from email import utils
File "/usr/local/lib/python3.4/email/utils.py", line 31, in <module>
import urllib.parse
File "/home/yuvi/pyth/urllib/parse.py", line 239, in <module>
_DefragResultBase.url.__doc__ = """The URL with no fragment identifier."""
AttributeError: readonly attribute
Try:
def img(url): full='name'+'.jpeg';urllib.urlretrieve(url,full)
urllib.request does not exist in Python 2.x, which seems to be your case
so don't try to import that in second line of your code
plus you made a typo (forgot semicolon) which works as a statement separator while writing inline function statements.
Similar to:
def img(url):
full='name'+'.jpeg'
urllib.urlretrieve(url,full)
I am using python 2.7 and urllib2 command for doing this.But I am facing error that urllib2 has no attribute name urlopen.Please help me.thanx,Here is my code.
import urllib2
import re
pat = re.compile('target="_parent">(.*?)</a>')
url = 'http://timesofindia.indiatimes.com/home/headlines'
sock = urllib2.urlopen(url)
li = pat.findall(sock.read())
sock.close()
print li
f=open("headlines.txt", 'a+')
for i in range(len(li)):
f.write(li[i]+"\n")
f.close()
Traceback
Error:Traceback (most recent call last):
File "C:/Users/Training/PycharmProjects/758702_Python_Program/ReadTOI/ReadTOI.py", line 1, in <module>
import urllib2
File "C:\Python27\lib\urllib2.py", line 111, in <module>
from urllib import (unwrap, unquote, splittype, splithost, quote,
File "C:\Users\Training\PycharmProjects\758702_Python_Program\urllib.py", line 4, in <module>
f=urllib.urlopen("http://www.python.org/")
AttributeError: 'module' object has no attribute 'urlopen'
File "C:\Users\Training\PycharmProjects\758702_Python_Program\urllib.py", line 4, in <module>
It looks like you have an other file by the name urllib.py in your working directory. When doing an import urllib python is importing your local file which does not contain urlopen(). Renaming your local file to something else or changing your working directory would solve this
I have come across a strange python module import issue.
When I trying to import the boilerpipe module,
from boilerpipe.extract import Extractor
I got this exception:
Original exception was:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/boilerpipe-1.2.0-py2.7.egg/boilerpipe/extract/ __init__.py", line 2, in <module>
import urllib2
File "/usr/lib/python2.7/urllib2.py", line 94, in <module>
import httplib
File "/usr/lib/python2.7/httplib.py", line 1140, in <module>
import ssl
File "/usr/lib/python2.7/ssl.py", line 58, in <module>
import textwrap
File "/usr/lib/python2.7/textwrap.py", line 40, in <module>
class TextWrapper:
File "/usr/lib/python2.7/textwrap.py", line 82, in TextWrapper
whitespace_trans = string.maketrans(_whitespace, ' ' * len(_whitespace))
AttributeError: 'module' object has no attribute 'maketrans'
I've searched over internet and saying that in Python 2.6 the 'str' module has been renamed to 'string' module. So this looks like some where in code library it didn't import "string" module properly.
Yet the really strange thing is, when I run the python code from home directory and run the same piece of code (either by using python shell or using python pyfile.py), it works fine! No more import error.
So I'm bit confusing. Can anyone give me any hint?
Thanks!
Some other script in sys.path is called "string.py" and is masking the stdlib module.
Double check to make sure that you don't have a file string.py that has been imported.
To debug this, put somewhere:
import sys
raise Exception("string module: %r" %(sys.modules.get("string"), ))
That will tell you what string module was imported (or if it shows None, no string module has been imported yet).