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)
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.
I am trying to make a discord bot, but I get the following weird error where it can not import asyncio.Transport, although I am able to use it normally outside this code.
The code:
import asyncio
import os
import discord
from dotenv import load_dotenv
The error message:
Traceback (most recent call last):
File "dis.py", line 1, in <module>
import asyncio
File "C:\Users\A.Gawish\AppData\Local\Programs\Python\Python36\lib\asyncio\__init__.py", line 21, in <module>
from .base_events import *
File "C:\Users\A.Gawish\AppData\Local\Programs\Python\Python36\lib\asyncio\base_events.py", line 19, in <module>
import inspect
File "C:\Users\A.Gawish\AppData\Local\Programs\Python\Python36\lib\inspect.py", line 36, in <module>
import dis
File "C:\Users\A.Gawish\dis.py", line 3, in <module>
import discord
File "C:\Users\A.Gawish\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\__init__.py", line 23, in <module>
from .client import Client
File "C:\Users\A.Gawish\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\client.py", line 34, in <module>
import aiohttp
File "C:\Users\A.Gawish\AppData\Local\Programs\Python\Python36\lib\site-packages\aiohttp\__init__.py", line 6, in <module>
from .client import (
File "C:\Users\A.Gawish\AppData\Local\Programs\Python\Python36\lib\site-packages\aiohttp\client.py", line 32, in <module>
from . import hdrs, http, payload
File "C:\Users\A.Gawish\AppData\Local\Programs\Python\Python36\lib\site-packages\aiohttp\http.py", line 7, in <module>
from .http_parser import (
File "C:\Users\A.Gawish\AppData\Local\Programs\Python\Python36\lib\site-packages\aiohttp\http_parser.py", line 14, in <module>
from .base_protocol import BaseProtocol
File "C:\Users\A.Gawish\AppData\Local\Programs\Python\Python36\lib\site-packages\aiohttp\base_protocol.py", line 4, in <module>
from .tcp_helpers import tcp_nodelay
File "C:\Users\A.Gawish\AppData\Local\Programs\Python\Python36\lib\site-packages\aiohttp\tcp_helpers.py", line 20, in <module>
def tcp_keepalive(transport: asyncio.Transport) -> None:
AttributeError: module 'asyncio' has no attribute 'Transport'
Your filename was dis.py, which is used in another module. I resolved a similar issue by ensuring the filename is different from any other existing files in Lib
If you look at your error messages, it is indicating Python36. If Transport in asyncio was only released in Python37, you need to change the pathing in whatever program you're using to write the code in as it's looking at Python36.
Testing it as the command prompt would show that your system sees Python37 but your code doesn't. Fix the pathing and it should work then.
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 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
When ever I try to import smtplib in the Python interpreter, I get this error:
ImportError: cannot import name fix_eols
How can I fix this?
Edit:
Here is the full stack trace:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 46, in <module>
import email.utils
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/email/utils.py", line 32, in <module>
from email._parseaddr import quote
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/email/_parseaddr.py", line 16, in <module>
import time, calendar
File "/Users/aaronblock/Documents/programming/scripts/calendar.py", line 7, in <module>
File "/usr/local/lib/python2.7/site-packages/apiclient/__init__.py", line 19, in <module>
from googleapiclient import discovery
File "/usr/local/lib/python2.7/site-packages/googleapiclient/discovery.py", line 38, in <module>
from email.generator import Generator
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/email/generator.py", line 15, in <module>
from email.header import Header
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/email/header.py", line 16, in <module>
import email.quoprimime
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/email/quoprimime.py", line 49, in <module>
from email.utils import fix_eols
ImportError: cannot import name fix_eols
Just to point out how to detect this kinds of errors (since it does happen from time to time):
Pay attention to the stacktrace. In this example, the problem can be seen in the line:
File "/Users/aaronblock/Documents/programming/scripts/calendar.py", line 7, in <module>
which certainly indicates wrong file being imported when we're trying to import a system-wide library.
I had a file called "calendar.py" which messes up my Python environment because smtplib needs calendar.py in order to work. Deleting calendar.py solved my problem.