Urllib2 Not installed? - python

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

Related

Python - installing urllib

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.

request_kerberos doesn't work

Are you able to tell me why I cannot use the request_kerberos module ? this is my code:
from django.http import HttpResponseNotFound ,HttpResponseRedirect
import requests
from requests_kerberos import HTTPKerberosAuth, OPTIONAL
I have already installed this module ,but when I'm opening the webpage I see that:
No module named winkerberos
I've checked the code and the problem is here ,kerberos.py file, first 4 lines:
try:
import kerberos
except ImportError:
import winkerberos as kerberos
winkerberos is for Windows OS and I'm using the CentOS
So, why my system cannot import that module ?

ImportError: cannot import name simplejson. I am using django v1.8 and django-Select2 v4.3.1

My error( command python manage.py runserver) - File "/Users/username/virtual-env/lib/python2.7/site-packages/jsonfield/fields.py", line 3, in
from django.utils import simplejson as json
ImportError: cannot import name simplejson
I tried solutions from these two posts but they did not work in my case Cannot import name simplejson - After installing simplejson
How to solve the ImportError: cannot import name simplejson in Django
If this error was in your own code, then you would simply change
from django.utils import simplejson as json
to
import json
after upgrading to Django 1.5 or later.
However, in your case, it looks like the problem is that you've have an old version of django-jsonfield installed. If you upgrade to the latest version (currently 1.03), it should fix the problem.
Use import json as simplejson instead
As of django 1.5, simplejson is no longer in django.utils module. So, just use python's JSON module instead.

AttributeError: 'module' object has no attribute 'request'

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.

python error while using urllib module

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.

Categories

Resources