I am trying to input a matix using numpy and I get the following error:
Traceback (most recent call last):
File "phase1.py", line 1, in <module>
import numpy as np
File "/home/rockstar/phase1.py", line 2, in <module>
a= np.array([1,2,3])
AttributeError: 'module' object has no attribute 'array'
I am not sure why this error is there. Could anyone help?
You probably have a file called numpy.py in the same folder that shadows the real numpy module. Rename your .py file and delete its .pyc file.
Related
So I've started to learn Python recently and right now I've been trying to learn arrays, but unable to use the array function after importing the array library.
I have tried four different methods to use the array function but failed successfully.
Method 1:
import array
nums = array.array('i', [])
#rest of the code
Output 1:
Traceback (most recent call last):
File "array.py", line 2, in <module>
import array
File "/home/prince/Desktop/python-basics/array.py", line
4, in <module>
nums = array.array('i', [])
TypeError: 'module' object is not callable
Method 2:
import array as a
nums = a.array('i', [])
#rest of the code
Output 2:
Traceback (most recent call last):
File "array.py", line 2, in <module>
import array as a
File "/home/prince/Desktop/python-basics/array.py", line
4, in <module>
nums = a.array('i', [])
AttributeError: partially initialized module 'array' has
no attribute 'array' (most likely due to a circular
import)
Method 3:
from array import array
nums = array('i', [])
#rest of the code
Output 3:
Traceback (most recent call last):
File "array.py", line 2, in <module>
from array import array
File "/home/prince/Desktop/python-basics/array.py", line
2, in <module>
from array import array
ImportError: cannot import name 'array' from partially
initialized module 'array' (most likely due to a circular
import) (/home/prince/Desktop/python-basics/array.py)
Method 4:
from array import *
nums = array('i', [])
Output 4:
Traceback (most recent call last):
File "array.py", line 2, in <module>
from array import *
File "/home/prince/Desktop/python-basics/array.py", line
4, in <module>
nums = array('i', [])
NameError: name 'array' is not defined
And after compilation, every time another folder is automatically created in my directory whose name is : pycache
And inside that folder there is a file named: array.cpython-38.pyc which I am unable to open. My editor says that it is because it either uses binary or unsupported text.
A few additional details if that helps:
Text Editor I Used: VS Code
My OS: Ubuntu 20.04LTS
Python Version: 3.8.5
All of the above imports fail due to the file name being same as the module name which you import. Pretty sure you can't have the same name as the module you're trying to import. Try renaming the filename array.py to something else and it should work.
About pycache folder, it contains the compiled bytecode for the python program. This shouldn't have anything to do with this problem.
class aSDAE_module():
def get_middle_layer(self,aSDAE,train_user):
middle=self.model.predict({'user_rating':aSDAE,'user_sideinformation':train_user},batch_size=self.batch_size)[2]
return middle
alpha = asdae_module.get_middle_layer(R.toarray(),aSDAE.toarray())
This is my piece of code where I am stuck, and I don't know how to resolve the following error:
Traceback (most recent call last):
File "./run.py", line 142, in <module>
train_user=train_user, train_item=train_item, valid_user=valid_user, test_user=test_user, R=R)
File "/home/hira/Desktop/PHD/PHDMF-master/asdae_models.py", line 52, in PHDMF
alpha = asdae_module.get_middle_layer(R.toarray(),aSDAE.toarray())
AttributeError: 'list' object has no attribute 'toarray'
There is no built-in type 'array' in python, it is not obvious what are you doing in asdae module, but you have to either change the implementation of asdae to work with list type object instead of an array or using Numpy library.
in order to converting list object to Numpy array you can do like this code:
import numpy as np
# converting aSDAE list to an array
aSDAE = np.array(aSDAE)
I'm using MacOs with the PyCharm environment, and my project is set to use Python 2.7.10. Now, I have on the first line of my module the line:
import numpy as np
When I try to run my code, I get the following error:
/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /{path to file}/numpy_exercise.py
Traceback (most recent call last):
File "/{path to file}/numpy_exercise.py", line 1, in module>
import numpy as np
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/__init__.py", line 169, in module>
from . import polynomial
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/polynomial/__init__.py", line 20, in module>
from .polynomial import Polynomial
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/polynomial/polynomial.py", line 68, in module>
from .polytemplate import polytemplate
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/polynomial/polytemplate.py", line 17, in module>
polytemplate = string.Template(''' AttributeError: 'module' object has no attribute 'Template'
(note that "module>" was actually in brackets from both sides but because it gets omitted that way I daleted the first bracket so you can see it)
Though in Python 3 it does not happens to me. I looked on the web but couldn't find anything. I have no clue of why this is happening. I'd be glad to get some of your help!
Thank you guys!
I am new to python.And i am learning the standard library.
Whenever i run the code below , it always raise the AttributeError...
And it seems like there is something wrong with the import command.
Also , i try to run it on the interactive interpreator,and it works just fine.
The sample code
import tempfile
import os
#temp = tempfile.TemporaryFile()
temp = tempfile.mktemp()
print "tempfile","=>",temp
file = open(temp,"w+b")
file.write("*" * 1000)
file.seek(0)
print len(file.read()),"byte"
file.close()
try:
os.remove(temp)
except OSError:
pass
The error output
Traceback (most recent call last):
File "tempfile.py", line 1, in <module>
import tempfile
File "/home/zhkzyth/codeRep/pytest/tempfile.py", line 5, in <module>
tempfile = tempfile.mktemp()
AttributeError: 'module' object has no attribute 'mktemp'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/apport_python_hook.py", line 66, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python2.7/dist-packages/apport/__init__.py", line 1, in <module>
from apport.report import Report
File "/usr/lib/python2.7/dist-packages/apport/report.py", line 12, in <module>
import subprocess, tempfile, os.path, urllib, re, pwd, grp, os
File "/home/zhkzyth/codeRep/pytest/tempfile.py", line 5, in <module>
tempfile = tempfile.mktemp()
AttributeError: 'module' object has no attribute 'mktemp'
Original exception was:
Traceback (most recent call last):
File "tempfile.py", line 1, in <module>
import tempfile
File "/home/zhkzyth/codeRep/pytest/tempfile.py", line 5, in <module>
tempfile = tempfile.mktemp()
AttributeError: 'module' object has no attribute 'mktemp'
My enviroment
ubuntu12.04
python2.7
Did you name your own file tempfile.py? If so, rename it, delete all your *.pyc files, and try again.
PS: providing the actual text of the error with the traceback would tell us these things.
Trying to access an attribute that does not belong to a class or function in a module raises an AttributeError exception, the attribute might have been deprecated in a later version of the Python interpreter being used. I suggest you check the version of the Python you're running and make sure your dir(module) includes the attribute you're trying to use
I am trying to read a *.wav file using scipy. I do the following:
import scipy
x = scipy.io.wavfile.read('/usr/share/sounds/purple/receive.wav')
As a result of this code I get:
Traceback (most recent call last):
File "test3.py", line 2, in <module>
x = scipy.io.wavfile.read('/usr/share/sounds/purple/receive.wav')
AttributeError: 'module' object has no attribute 'io'
Does anybody know what is wrong here? Thank you in advance.
As the error says, scipy module does not have 'io'.
io.wavfile is a submodule, you need to from scipy.io import wavfile and then do wavfile.read("/usr/share/sounds/purple/receive.wav")
This gives me an error with the file you are using as an example, however...