Python3 urllib import is failing - python

So after trying much to do this on my own and failing due to a syntax error, I tried directly copying a code that was proven to work on another computer just to get the same syntax error. Am I missing some files? What is my issue here?
Here is the code
import urllib.request
fhand = urllib.request.urlopen('http://data.pr4e.org/romeo.txt')
for line in fhand:
print(line.decode().strip())
The error I constantly see is this:
File "/Users/JongWhoLee/Desktop/python/ex_15.py", line 1, in <module>
from urllib.request import urlopen
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 88, in <module>
import http.client
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 71, in <module>
import email.parser
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/email/parser.py", line 12, in <module>
from email.feedparser import FeedParser, BytesFeedParser
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/email/feedparser.py", line 27, in <module>
from email._policybase import compat32
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/email/_policybase.py", line 7, in <module>
from email import header
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/email/header.py", line 16, in <module>
import email.quoprimime
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/email/quoprimime.py", line 44, in <module>
from string import ascii_letters, digits, hexdigits
File "/Users/JongWhoLee/Desktop/python/string.py", line 1
>>> import string
^
SyntaxError: invalid syntax
Is there something wrong with my computer or is it just me?

Related

Python urllib AttributeError

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 testing HTTP get method but there is an error "No module named ASCII"?

ASCII is installed but still getting this error. I tested by importing all the libraries in the error msg but could not solve it.
Complete Error Message here.
Traceback (most recent call last):
File "re.py", line 1, in <module>
import requests,compile, ASCII
File "C:\Program Files\Python37\lib\site-packages\requests\__init__.py", line 43, in <module>
import urllib3
File "C:\Program Files\Python37\lib\site-packages\urllib3\__init__.py", line 7, in <module>
from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
File "C:\Program Files\Python37\lib\site-packages\urllib3\connectionpool.py", line 3, in <module>
import logging
File "C:\Program Files\Python37\lib\logging\__init__.py", line 26, in <module>
import sys, os, time, io, traceback, warnings, weakref, collections.abc
File "C:\Program Files\Python37\lib\traceback.py", line 5, in <module>
import linecache
File "C:\Program Files\Python37\lib\linecache.py", line 11, in <module>
import tokenize
File "C:\Program Files\Python37\lib\tokenize.py", line 33, in <module>
import re
File "C:\Users\naeem\Desktop\re.py", line 1, in <module>
import requests,compile, ASCII
ModuleNotFoundError: No module named 'ASCII'
Python Code I am running is here:
import requests,compile, ASCII
def main():
res = requests.get("https://www.google.com/")
print(res.text)
if __name__=="__main__":
main()
Can anybody help me out please?
ModuleNotFoundError: No module named 'ASCII'
This means that don't exist a module named 'ASCII'.
Maybe it's useful curses.ascii
so:
import curses.ascii
You can find Documentation here:
https://docs.python.org/3/library/curses.ascii.html

urllib doesn't work properly

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.

Python request.py syntax error

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)

Cannot `import smtplib`

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.

Categories