I'm trying to use urllib (for backward compatibility), but I always get an error when running the script.
I tried installing urllib but got this error
ERROR: Could not find a version that satisfies the requirement urllib (from versions: none)
ERROR: No matching distribution found for urllib
Python version - 2.7.16
this is the import part in the script which fails:
from urllib import request, parse
from urllib import error as urllib_error
import error:
ImportError: cannot import name request
Please advise.
From the 2.7 documentation :
The urllib module has been split into parts and renamed in Python 3 to urllib.request, urllib.parse, and urllib.error.
So if you are trying to import urllib.request, parse and error separately - that constructs are available in Python 3.x version.
Please go through the documentation for 2.7 at : https://docs.python.org/2.7/library/urllib.html
Or upgrade to latest verion of Python 3.x to import the way you have described in your post.
You should import like this:
Download latest version of python from here
import urllib.request,urllib.parse, urllib.error
There is no urllib.request module in Python 2, that module only exists in Python 3, You can use urllib2 and Request:
from urllib2 import Request
The issue was that my environment was configured to use Python2 and not Python3, once I resolved this, the import worked as expected.
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 setup virtualenv using python 3.4.3 and tried to import JSONDecodeError from json.decoder
from json.decoder import JSONDecodeError (I think its valid in python3 ? But why not working for me ?)
But it is not working. Only below is working:
from simplejson import JSONDecodeError
How I did ?
virtualenv venv --no-site-packages -p python3
pip install ipython
ipython
from json.decoder import JSONDecodeError
ImportError: cannot import name 'JSONDecodeError'
According to 3.4.x docs, plain ValueError is raised when JSON decoding fails.
JSONDecodeError class is available starting from 3.5.x.
According to Docs from module json (Python version >= 3.5.0), Python which version < 3.5.0 does not support import statement like what you just did, but if you use Python(version>=3.5.0), your import statement is definitely correct.
json is the version of simplejson that has been integrated into Python. They have since been developed separately and are not the same anymore. So they cannot necessarily be used interchangably.
See this answer for more details about the differences.
I am tying to modify the original sklearn.CalibrationCV to create my won version. The original code has "from .utils.fixes import signature". So I did the following in my version:
from sklearn.utils.fixes import signature
but got a error:
ImportError: cannot import name signature
When check the sklearn source code on GitHub. I see the following code inside fixes.py:
try:
from inspect import signature
except ImportError:
from ..externals.funcsigs import signature
Then I did from inspect import signature directly. Still get "ImportError: cannot import name signature"
Besides how to fix this, I am also curious about why the original version can import a module that will be imported from another source? Thanks.
In python 2, the inspect module does not have a signature method.
In python 3, the inspect module does have a signature method.
This code is just trying to work with both python 2 and 3.
You may want to use the funcsigs module if you are using python 2, or use sklearn.externals.funcsigs directly (for version sklearn >= 0.17).
The accepted answer doesn't work with the latest version of sklearn.
Please install funcsigs directly using
pip install funcsigs
and use from funcsigs import signature instead.
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.
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.