0

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
Yuva
  • 43
  • 7

1 Answers1

1

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)
Melebius
  • 6,183
  • 4
  • 39
  • 52
Yogesh
  • 453
  • 1
  • 4
  • 12
  • Huh? Has anyone (apart from you) ever mentioned inline functions here? – Melebius Feb 21 '17 at 08:16
  • @Melebius you should see the `edited` tag on the question. Prior to edit, it was inline function so had to answer in that form. – Yogesh Feb 26 '17 at 14:35
  • I had opened the post history and found nothing about inline function before I wrote my previous comment. – Melebius Feb 27 '17 at 07:13
  • @Melebius Well, you could've done better at finding. [have a look](http://askubuntu.com/posts/812417/revisions#spacer-53dbd0aa-9a24-4664-a664-82dc257cad63) – Yogesh Feb 28 '17 at 15:32