ImportError: cannot import name 'loads' from 'json' (unknown location) - python

Previos title was:
AttributeError: module 'json' has no attribute 'loads'
I changed it because it looks similar to this but at the link that i provided, the problem seems that the person was having a file called json.py that was tricking the import to think that is importing a local file and not the json from standard library. My problem is that I don't have any local file called json.py;
I am wondering if it has to do anything related to PATH or the structure of my project. Any suggestion might help.
error traceback:
File "D:\Me\IdeaProjects\src\app\repositories\user_repository.py", line 14, in get_user
user = json.loads(file.read())
I am running the code in Windows 10, and intelliJ ide.
Python version: 3.7.4
Tried the code from the official documentation this:
import json
def as_complex(dct):
if '__complex__' in dct:
return complex(dct['real'], dct['imag'])
return dct
json.loads('{"__complex__": true, "real": 1, "imag": 2}', object_hook=as_complex)
And got this error as well:
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
json.loads('{"__complex__": true, "real": 1, "imag": 2}',object_hook=as_complex)
AttributeError: module 'json' has no attribute 'loads'
When I try to import loads explicitly i get this error:
ImportError: cannot import name 'loads' from 'json' (unknown location)

I had python installed in Admin account in window 10 and it was installed with Admin privileges, but when i used in another account I could not use the packages, however installing the python in the current account did fix the problem.

Try to import loads explicitly:
import json
from json import loads

Related

AttributeError: module 'collections' has no attribute 'Iterable'

I am using the "pdftables" library to extract tables from a pdf.
This is my code:
import pdftables
pg = pdftables.get_pdf_page(open("filename.pdf","rb"),253)
print(pg)
table = pdftables.page_to_tables(pg)
print(table)
I am getting this error and I am not sure what's causing it.
Traceback (most recent call last):
File "c:\Users\gayak\OneDrive\Documents\PDF to Database\PDF_to_Tables_3.py", line 9, in <module>
table = pdftables.page_to_tables(pg)
File "C:\Users\gayak\AppData\Local\Programs\Python\Python310\lib\site-packages\pdftables\pdftables.py", line 485, in page_to_tables
box_list = LeafList().populate(page, flt).purge_empty_text()
File "C:\Users\gayak\AppData\Local\Programs\Python\Python310\lib\site-packages\pdftables\tree.py", line 98, in populate
for obj in children(pdfpage):
File "C:\Users\gayak\AppData\Local\Programs\Python\Python310\lib\site-packages\pdftables\tree.py", line 75, in children
if isinstance(obj, collections.Iterable):
AttributeError: module 'collections' has no attribute 'Iterable'
The version of python I am using is python 3.10.4
I used pip install pdftables.six to get the library
If you don't want to change the source code, there is an easier way. Just use this in your script after importing.
import collections
collections.Iterable = collections.abc.Iterable
As the Error says, the attribute isn't valid. When using collection.Iterable then it not finds the Iterable attribute. This can have different reasons but in this case, after looking on the package files on Github, i noticed that you are missing the abc keyword. I not tried this solution, but I'm 95% sure that using collections.abc.Iterable will do the thing.
import collections
from _collections_abc import Iterable
collection.Iterable = Iterable
This should work 🙂
A simple fix that works for python3.10:
Under directory
/usr/lib/python3.10/collections/__ init__.py
Note: The path might change depending
Add this line of code:
from _collections_abc import Iterable

AttributeError: module 'numpy' has no attribute 'array

I do not have a file named numpy.py.
My exact steps are: in Python, Open C:\Adrian\Python37\Lib\numpy-1.11.2\setup.py, I run this module:
>>>
=========== RESTART: C:\Adrian\Python37\Lib\numpy-1.11.2\setup.py ===========
Running from numpy source directory.
>>> import numpy as np
>>> list_int = [8, 3, 34, 111]
>>> a_int = np.array(list_int)
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
a_int = np.array(list_int)
AttributeError: module 'numpy' has no attribute 'array'
In English : I had encountered the same problem as you, and as for me the cause of the problem was that I had create a test file with the name of numpy.py in the root of my project question to make tests. So by importing the numpy library, I had to know this bug because python imported my test file instead of the library.
So you have to start by checking whether the name of your project or a file other than the library has the name of the library to import. be sure that none of your files with the name of your project respond to a name from a library.
Reported symptom is:
>>> import numpy as np
>>> a_int = np.array(list_int)
...
AttributeError: module 'numpy' has no attribute 'array'
This can't possibly happen if numpy is correctly installed.
The OP explains that re-installing numpy properly on his machine solves the symptom.
Even I created a file with name "numpy.py". This file is imported itself.
changed the file name.

Any way to see where python is importing a module from?

So I installed the facebook module, realized it was the wrong one, used pip to uninstall and then installed facebook-sdk. Here is my code:
import facebook
token = '[token]'
graph = facebook.GraphAPI(token)
profile = graph.get_object("me")
friends = graph.get_connections("me", "friends")
friend_list = [friend['name'] for friend in friends['data']]
print friend_list
and get
Traceback (most recent call last):
File "C:\Users\mgraves\Desktop\facebook.py", line 1, in <module>
import facebook
File "C:\Users\mgraves\Desktop\facebook.py", line 5, in <module>
graph = facebook.GraphAPI(token)
AttributeError: 'module' object has no attribute 'GraphAPI'
When looking this up, EVERY result says to uninstall facebook and facebook-sdk and reinstall facebook-sdk. And I have, many many times. I searched /python27/ for facebook afterwards to make sure the files were gone.
Is there any way on a windows machine to trace back where I am importing "facebook" from?
Module objects have a __file__ attribute, and the object representation also includes the file:
print facebook
print facebook.__file__
In your case, you are importing your own script; you named it facebook as well and are masking the installed module:
File "C:\Users\mgraves\Desktop\facebook.py", line 1, in <module>
import facebook
File "C:\Users\mgraves\Desktop\facebook.py", line 5, in <module>
graph = facebook.GraphAPI(token)
Note the filename in the first line, then the fact that the same file is used for that import. Python stores the main script as __main__, so importing the script itself results in another module being created for the actual filename.

Import Error using cPickle in Python

I am using Pickle in Python2.7. I am getting error while using cPickle.load() method. The code and error is shown below. Can someone guide me through this?
Code:
#! usr/bin/python
import cPickle
fo = open('result','rb')
dict1 = cPickle.load(fo)
Error:
Traceback (most recent call last):
File "C:\Python27\test.py", line 7, in <module>
dicts = cPickle.load(fo)
ImportError: No module named options
It seems like you can not do
import options
but when you or someone else did
cpickle.dump(xxx, open('result', 'rb'))
there was an object with a class or function of a module options that existed at this point in time, in xxx.
Solution
You can open the file binarily and replace options with the module you replaced the old module options with.
You probably created the file in your package like in module package.main by executing the file main.py or something like it, having a module options in the same directory.
Now you do import package.main, try to read the file and options is now called package.options and the module options can not be found.
How did you create this file? How do you load it now? cPickle/pickle does not transfer source code - so if you use a function you need the module when you load it.

Jira-Python - jira.client import error

I was installing jira-python like written in the docs
$ pip install jira-python
but after installation I try to run the example:
from jira.client import JIRA
options = {
'server': 'https://jira.atlassian.com'
}
jira = JIRA(options)
projects = jira.projects()
keys = sorted([project.key for project in projects])[2:5]
issue = jira.issue('JRA-1330')
import re
atl_comments = [comment for comment in issue.fields.comment.comments
if re.search(r'#atlassian.com$', comment.author.emailAddress)]
jira.add_comment(issue, 'Comment text')
issue.update(summary="I'm different!", description='Changed the summary to be different.')
issue.delete()
getting the following error:
**Traceback (most recent call last):
File "jira.py", line 4, in <module>
from jira.client import JIRA
File "/home/ubuntu/jira.py", line 4, in <module>
from jira.client import JIRA
ImportError: No module named client**
Any idea about the problem here? I tried it also on an Amazon instance, but same problem...
seems like the reason was that my test file was named jira.py :) thanks for your help Inbar!

Categories

Resources