AttributeError: module 'numpy' has no attribute 'array - python

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.

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

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

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

TypeError: 'module' object is not subscriptable

I'm using python version 3.6.
mystuff.py includes:
mystuff = {'donut': "SHE LOVES DONUTS!"}
mystuffTest.py includes this
import mystuff
print (mystuff['donut'])
The error that I receive when I run mystuffTest.py is as follows:
$ python3.6 mystuffTrythis.py
Traceback (most recent call last):
File "mystuffTrythis.py", line 3, in <module>
print (mystuff['donut'])
TypeError: 'module' object is not subscriptable
So far I haven't seen this exact error here on stackoverflow. Can anyone explain why I am getting this error?
import mystuff is importing the module mystuff, not the variable mystuff. To access the variable you'd need to use:
import mystuff
print(mystuff.mystuff['donut'])
EDIT: It's also possible to import the variable directly, using:
from mystuff import mystuff
print(mystuff['donut'])
I got this error because a later from __ import * statement imported a module which bound my variable to something else:
from stuff_a import d
from stuff_b import *
d['key']
In stuff_b.py, d was bound to a module, hence the error. Lesson learned: avoid importing * from modules.

Where can I locate the library for python base functions in Linux?

I am mainly trying to get examples from the library of the base functions to help aid me in my studies of Python. I am running Linux Mint 17 and I would like to simply know the path to the base functions, so I can open them and view the Python code they contain.
Each non built-ins modules have a __file__ attribute. He contains the fullpath of the loaded file, so if it's a module write in python, you will get a '.pyc' file, if it's a C module a '.so'.
>>> import collections # from the std lib in pure python
>>> collections.__file__
'/usr/lib/python2.7/collections.pyc'
>>> import datetime # from the std lib as a C module
>>> datetime.__file__
'/usr/lib/python2.7/lib-dynload/datetime.so'
>>> import itertools # from the std lib but a built-in module
>>> itertools.__file__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'
You can also use the inspect module, who have a .getsourcefile function, this work not only at module level but a function level too. If the function is declared in python !
>>> import inspect
>>> inspect.getsourcefile(collections) # Pure python
'/usr/lib/python2.7/collections.py'
>>> inspect.getsourcefile(collections.namedtuple) # Work with a function too.
'/usr/lib/python2.7/collections.py'
>>> inspect.getsourcefile(datetime) # C module so it will return just None
>>> inspect.getsourcefile(itertools) # Built-in so it raise an exception
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/inspect.py", line 444, in getsourcefile
filename = getfile(object)
File "/usr/lib/python2.7/inspect.py", line 403, in getfile
raise TypeError('{!r} is a built-in module'.format(object))
TypeError: <module 'itertools' (built-in)> is a built-in module
Has you can see, if it's a external C library, .getsourcefile return nothing. And if it's a built-in module/function/class, it raise a TypeError exception.
The others advantages of .getsourcefile over __file__ is that if the function/class is declared in a subfile of the module it return the right file. And you can even use it on the type of a "an unknown" object and do inspect.getsourcefile(type(obj)).
(It's test if the source file exist too and return None if the '.pyc' is loaded but the '.py' doesn't exist)
The base install is usually in /usr/lib{,64}/python*/, and installed packages in /usr/lib{,64}/python*/site-packages:
ch3ka#x200 /tmp % locate this.py | grep python3\.4
/usr/lib64/python3.4/this.py
(this is the path to the module this - substitute 3\.4 with your python version, or simply skip the | grep)
But when using virtualenv, your PYTHONPATH may be anywhere, depends on what you specified when creating the virtualenvs. Use locate(1) and your judgement.

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.

Categories

Resources