When I run the following code in Python 3.3:
import urllib
tempfile = urllib.request.urlopen("http://yahoo.com")
I get the following error:
I did this too to verify:
What am I doing wrong?
The urllib module has been split into parts and renamed in Python 3 to urllib.request, urllib.parse, and urllib.error.
Import urllib.request instead of urllib.
import urllib.request
Interestingly, I noticed some IDE-depending behavior.
Both Spyder and PyCharm use the same interpreter on my machine : in PyCharm I need to do
import urllib.request
while in Spyder,
import urllib
does fine
If this is on PyCharm, as was mine, make sure your file name isn't urllib.py.
In visual code , u have to write import urllib.request instead of just import urllib.
Also, whenever errors such as module x has no attribute y occurs, it's because you have named the current file same as the package you are trying to import.
So, the way import in python works is that it first searches the current dir, and if it finds the module/package 'x' u were looking for , it assumes that it has found the target file, and searches for 'y'. And since u haven't defined 'y', the aforementioned error occurs.
Related
I coded this:
from datetime import datetime, timedelta
import json
import time
import urllib2
...
req=urllib2.Request(api_url,binary_data,header)
f=urllib2.urlopen(req)
My python version is the 3.6.5 so i´m supposed to have the urllib2 installed already but every time i get this error:
import urllib2
ModuleNotFoundError: no module named 'URLLIB2'
I changed the name to urllib3 as it appears in my anaconda folder but it crashes anyway....
what do i do?
Urllib2 is meant for Python 2, it is no longer used in Python 3. The standard module is now called urllib (you can find the documentation for it here: https://docs.python.org/3/library/urllib.html).
Try this instead:
import urllib.request
req=urllib.request.Request(api_url,binary_data,header)
f=urllib.request.urlopen(req)
urllib.request.Request(): https://docs.python.org/3/library/urllib.request.html#urllib.request.Request
urllib.request.urlopen():https://docs.python.org/3/library/urllib.request.html#urllib.request.urlopen
I want to import a color module but I am getting the error:
ImportError: No module named 'scripts.UltraColor'
Import code:
from scripts.UltraColor import *
The import code is in pygameTest.py
The problem here, as far as I can see on that screenshot is UltraColor file doesn't have the .py extension, for import to work properly you'll need to rename it from UltraColor to UltraColor.py, otherwise will raise an ImportError exception.
For more information, take a look to this one. Import a python module without the .py extension
I have been trying to run my codes :
(neaweather.py)
import requests
from bs4 import BeautifulSoup
import urllib3
r = requests.get('http://www.nea.gov.sg/api/WebAPI/?
dataset=2hr_nowcast&keyref=<keyrefno>')
soup = BeautifulSoup(r.content, "xml")
soup.find('validTime').string
However, when I run the codes, this is the error I got :
Error on CMD
I used to have a file called "urllib.py" (located on C:\scripts along with the file which im running neaweather.py) which clashes with the python modules however I have deleted the file. I have also deleted the file "urllib.pyc" from C:\scripts as well.
I have also deleted files for python3 which I have installed previously as im using python 2.7.12
I tried googling about this error and I saw a comment saying that .pyc files has something to do with this error. Is this true as I have already deleted the file "urllib.pyc" on C:\scripts
I'm not sure on how to solve this error, anyone?
This is not a duplicate of another similar error as I donot have a file name that clashes with python built in modules anymore as I have deleted it.
Thanks
From a Python prompt:
>>> import urllib
>>> print urllib.__file__
Make sure the file is part of the Python distribution and not your own script.
I was making python http banner grabber using urllib but it gives me an error.
I was not able it import urllib2 (module not found error) so tried with with urllib instead.
Here's the code , what's wrong with it?
#!/usr/bin/env python
import urllib
url=urllib.urlopen('www.bing.com')
print (url.info())
Error:
AttributeError: 'module' object has no attribute 'urlopen'
You are using Python 3; use urllib.request instead.
import urllib.request
url = urllib.request.urlopen('http://www.bing.com')
print(url.info())
This is documented at the top of the urllib2 page:
Note: The urllib2 module has been split across several modules in Python 3 named urllib.request and urllib.error. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.
I have looked everywhere for this and just cannot find the answer. I have checked my python version and it is version 3.2 . When I try to import cookielib I receive:
ImportError: No module named cookielib
I have seen that in Python 3.0 it was renamed to
http.cookiejar and that it would auto import cookielib.
I thought that maybe there was some wild error in my python configuration so I thought I should try and import http.cookiejar like this import http.cookiejar. That did not work all and I get and error:
EOFError: EOF read where not expected.
This is not the error I had expected becuase import http.cookies imports just fine.
Does anybody have a solution to this problem? What am I overlooking?
Full Error:
Traceback (most recent call last):
File "C:\Users\Spencer\Downloads\selenium-2.20.0.tar\selenium-2.20.0\selenium-2.20.0\test", line 1, in <module>
import urllib.request, urllib.parse, http.cookiejar
EOFError: EOF read where not expected
The automatic renaming business only applies if you use 2to3. Therefore, you have to import http.cookiejar.
The error EOFError: EOF read where not expected is only ever thrown by Python marshalling. Most likely, this is caused by a race condition fixed in Python 3.3, where multiple processes tried to write concurrently to the pyc file. Deleting all .pyc files may be a workaround.
try:
import cookielib
except:
import http.cookiejar
cookielib = http.cookiejar
The cookielib module has been renamed to http.cookiejar in Python 3.0. The 2to3 tool will automatically adapt imports when converting your sources to 3.0.
My initial guess is that you have a corrupted library file. Inside your Python installation, look at lib/python3.2/http/cookiejar.py and scroll down to the end. Mine (Python 3.2.2) ends in the save() method definition with
finally:
f.close()
If you see anything else, your installation is probably broken and I'd recommend reinstalling it.