I am new to python and just learning by watching videos from certain blogs. This is a program of downloading an image from internet which shows the following error
import random
import urllib.request
def download_web_image(url):
name = random.randrange(1, 1000)
full_name = str(name) + ".jpg"
urllib.request.urlretrieve(url, full_name)
download_web_image("https://www.thenewboston.com/images/homepage_images /main_homepage_01.png")
Error:
Traceback (most recent call last):
File "bucky.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
Thank you in advance
Related
import urllib.request
fhand = urllib.request.urlopen("http://www.python.org")
for line in fhand:
print(line.decode().strip())
This code isn't working, I'm using python3 on windows10. Every time I run this code in command prompt using atom editor, I get the following error message:
Traceback (most recent call last):
File "C:\Users\isha\Downloads\py4e\trial.py", line 1, in <module>
import urllib.request
File "C:\Users\isha\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 88, in <module>
import http.client
File "C:\Users\isha\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 71, in <module>
import email.parser
File "C:\Users\isha\AppData\Local\Programs\Python\Python38-32\lib\email\parser.py", line 12, in <module>
from email.feedparser import FeedParser, BytesFeedParser
File "C:\Users\isha\AppData\Local\Programs\Python\Python38-32\lib\email\feedparser.py", line 27, in <module>
from email._policybase import compat32
File "C:\Users\isha\AppData\Local\Programs\Python\Python38-32\lib\email\_policybase.py", line 9, in <module>
from email.utils import _has_surrogates
File "C:\Users\isha\AppData\Local\Programs\Python\Python38-32\lib\email\utils.py", line 29, in <module>
import socket
File "C:\Users\isha\Downloads\py4e\socket.py", line 5, in <module>
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
**AttributeError: partially initialized module 'socket' has no attribute 'AF_INET' (most likely due to a circular import)**
Help me with this code, also I'm not able to run urllib.request and related programs
Your code works well for me. Try delete 'C:\Users\isha\Downloads\py4e\socket.py' this file and run your code again.
This question already has answers here:
'module' object has no attribute 'loads' while parsing JSON using python [duplicate]
(2 answers)
Importing installed package from script with the same name raises "AttributeError: module has no attribute" or an ImportError or NameError
(2 answers)
Closed 3 years ago.
I am trying to import Numpy
import numpy as np
but I am getting this error:
AttributeError: module 'linecache' has no attribute 'checkcache'
Full trace:
Traceback (most recent call last):
File "C:/Users/z1885/Desktop/鸡行为分析/codepy/ML/KNN.py", line 1, in <module>
import numpy as np
File "C:\Users\z1885\Desktop\bouk\venv\lib\site-packages\numpy\__init__.py", line 145, in <module>
from . import lib
File "C:\Users\z1885\Desktop\bouk\venv\lib\site-packages\numpy\lib\__init__.py", line 24, in <module>
from .npyio import *
File "C:\Users\z1885\Desktop\bouk\venv\lib\site-packages\numpy\lib\npyio.py", line 14, in <module>
from ._datasource import DataSource
File "C:\Users\z1885\Desktop\bouk\venv\lib\site-packages\numpy\lib\_datasource.py", line 41, in <module>
import shutil
File "C:\Users\z1885\Desktop\bouk\venv\lib\shutil.py", line 16, in <module>
import bz2
File "C:\Users\z1885\AppData\Local\Programs\Python\Python35\Lib\bz2.py", line 18, in <module>
from threading import RLock
File "C:\Users\z1885\AppData\Local\Programs\Python\Python35\Lib\threading.py", line 7, in <module>
from traceback import format_exc as _format_exc
File "C:\Users\z1885\AppData\Local\Programs\Python\Python35\Lib\traceback.py", line 5, in <module>
import linecache
File "C:\Users\z1885\Desktop\bouk\venv\lib\linecache.py", line 11, in <module>
import tokenize
File "C:\Users\z1885\Desktop\bouk\venv\lib\tokenize.py", line 34, in <module>
from token import *
File "C:\Users\z1885\Desktop\鸡行为分析\codepy\token.py", line 5, in <module>
import request as request
File "C:\Users\z1885\Desktop\bouk\venv\lib\site-packages\request\__init__.py", line 2, in <module>
from get import GET
File "C:\Users\z1885\Desktop\bouk\venv\lib\site-packages\get\__init__.py", line 3, in <module>
from query_string import query_string
File "C:\Users\z1885\Desktop\bouk\venv\lib\site-packages\query_string\__init__.py", line 18, in <module>
#public.add
File "C:\Users\z1885\Desktop\bouk\venv\lib\site-packages\public\__init__.py", line 65, in add
return _add(_caller_modules()[1], objects)
File "C:\Users\z1885\Desktop\bouk\venv\lib\site-packages\public\__init__.py", line 12, in _caller_modules
frames = inspect.getouterframes(inspect.currentframe())
File "C:\Users\z1885\AppData\Local\Programs\Python\Python35\Lib\inspect.py", line 1441, in getouterframes
frameinfo = (frame,) + getframeinfo(frame, context)
File "C:\Users\z1885\AppData\Local\Programs\Python\Python35\Lib\inspect.py", line 1414, in getframeinfo
lines, lnum = findsource(frame)
File "C:\Users\z1885\AppData\Local\Programs\Python\Python35\Lib\inspect.py", line 747, in findsource
linecache.checkcache(file)
AttributeError: module 'linecache' has no attribute 'checkcache'
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.
Traceback (most recent call last):
File "parseVar.py", line 1, in <module>
from pygccxml import utils
File "/usr/local/lib/python2.7/dist-packages/pygccxml/__init__.py", line 29, in <module>
from . import declarations
File "/usr/local/lib/python2.7/dist-packages/pygccxml/declarations/__init__.py", line 16, in <module>
from .declaration import declaration_t
File "/usr/local/lib/python2.7/dist-packages/pygccxml/declarations/declaration.py", line 13, in <module>
from . import algorithms_cache
File "/usr/local/lib/python2.7/dist-packages/pygccxml/declarations/algorithms_cache.py", line 11, in <module>
from .. import utils
File "/usr/local/lib/python2.7/dist-packages/pygccxml/utils/__init__.py", line 11, in <module>
from .utils import is_str
File "/usr/local/lib/python2.7/dist-packages/pygccxml/utils/utils.py", line 12, in <module>
import tempfile
File "/usr/lib/python2.7/tempfile.py", line 35, in <module>
from random import Random as _Random
File "/home/jayesh/Desktop/random.py", line 10, in <module>
print random.uniform(1.2, 2.5)
AttributeError: 'module' object has no attribute 'uniform'
Can somebody please suggest how to fix this 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)