Python3: unable to import JSONDecodeError from json.decoder - python

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.

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.

no simple json module in python 3.7

I am trying import 'simplejson'module into in my pycharm project. It is throwing an error saying that import simplejson as json
ModuleNotFoundError: No module named 'simplejson'
I installed simple json in the terminal using pip3 install simplejson.
I have tried not renaming it as json but that didn't work.

Not able to import module after installing using pip

Im trying to import a module called geoip2 from pypi into python it is not included in its standard libraries.
I open command prompt and type:
pip install geoip2
The command prompt returns
Successfully installed geoip2-2.4.2
After it is installed I try importing it using IDLE:
import geoip2.webservice
which returns the error:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import geoip2.webservice
ImportError: No module named 'geoip2'
Although it is installed already I cannot use it. How can i prevent this? Take note that I use python 3.6
May be you have two different version of Python installed. Try opening IDLE using the Python version where you have installed geoip.
Instead of:
import geoip2.webservice
Try doing:
import geoip2
from geoip2 import webservice
Since geoip2.webservice is not installed, geopip2 is and .webservice is an function object of that module.
Further, you can avoid typing geoip2.webservice every time by doing:
import geoip2
from geoip2 import webservice as gws
Then anytime you want to run the .webservice function, you can just use gws.
Alternatively:
Just do:
import geoip2
Then in your script you can call it:
geoip2.webservice(#do stuff here or however you call the function)

ImportError: cannot import name signature

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.

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.

Categories

Resources