This happens because the scipy module doesn't have any attribute named sparse. That attribute only gets defined when you import scipy.sparse.
Submodules don't automatically get imported when you just import scipy; you need to import them explicitly. The same holds for most packages, although a package can choose to import its own submodules if it wants to. (For example, if scipy/__init__.py included a statement import scipy.sparse, then the sparse submodule would be imported whenever you import scipy.)
This happens because the scipy module doesn't have any attribute named sparse. That attribute only gets defined when you import scipy.sparse.
Submodules don't automatically get imported when you just import scipy; you need to import them explicitly. The same holds for most packages, although a package can choose to import its own submodules if it wants to. (For example, if scipy/__init__.py included a statement import scipy.sparse, then the sparse submodule would be imported whenever you import scipy.)
Because you imported scipy, not sparse. Try from scipy import sparse?
Videos
Your constructor sets self.prevNode to None, and later you try to access node.prevNode.label, which is like trying to access None.label. None doesn't have any attributes, so trying to access any will give you an AttributeError.
As per the other answers (and the error message) you are accessing None.label. If it is expected that node might be None, then you will need to check for it before appending it.
while node.label != node.prevNode.label:
node = node.prevNode
if node is not None:
labels.append(node.label)
And what does that error generally mean?
An Attribute in Python is a name belonging to an object - a method or a variable. An AttributeError means that the program tried to use an attribute of an object, but the object did not have the requested attribute.
For instance, string objects have the 'upper' attribute, which is a method that returns the uppercase version of the string. You could write a method that uses it like this:
def get_upper(my_string):
return my_string.upper()
However, note that there's nothing in that method to ensure that you have to give it a string. You could pass in a file object, or a number. Neither of those have the 'upper' attribute, and Python will raise an Attribute Error.
As for why you're seeing it in this instance, you haven't provided enough detail for us to work it out. Add the full error message to your question.
1) Put code in Try ... Except block. get exception details.
2) Could you tell StackTrace details means which line # and method thrown error
And also are you able to run other simple python scripts without any error. Means just try to run some sample script etc.
I'm curious... how much do you know about scope in Python?
In your class, you have a member variable named x and another named y. Your init function accepts an argument called point0 and another called point1. It saves point0 in the x member variable, and point1 in y. Then, in your rotate function, you attempt to access a variable called point0. Do you see the problem?
An important thing to understand when programming (and this is true in most programming languages, if not all of them) is that the name of an argument doesn't affect the name of that data elsewhere. I can pass a variable called foo into a function that takes an argument called bar. In that function, I have to refer to the data as bar because that's the name of the variable. Later, after I've called that function, the name of the variable is still foo, because only the variable inside the function is called bar. Does that make sense?
your class accept point0 and point1 parameters when you call it. If you want to get values of these parameters you should use self.x(for point0) and self.y(for point1)
or another way;
class Line:
def __init__(self, point0, point1):
self.point0 = point0
self.point1 = point1
I suggest you to read;
Python __init__ and self what do they do?
https://www.ibiblio.org/swaroopch/byteofpython/read/class-init.html
https://docs.python.org/2/tutorial/classes.html
AttributeError typically identifies the missing attribute. e.g.:
class Foo:
def __init__(self):
self.a = 1
f = Foo()
print(f.a)
print(f.b)
When I run that, I see:
$ python foo.py
1
Traceback (most recent call last):
File "foo.py", line 10, in <module>
print(f.b)
AttributeError: Foo instance has no attribute 'b'
That's pretty explicit. If you're not seeing something like that, please post the exact error you're seeing.
EDIT
If you need to force the printing of an exception (for whatever reason), you can do this:
import traceback
try:
# call function that gets AttributeError
except AttributeError:
traceback.print_exc()
That should give you the full error message and traceback associated with the exception.
The traceback should alert you to the attribute access that raised the AttributeError exception:
>>> f.b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: Foo instance has no attribute 'b'
Alternatively, convert the Exception to str:
>>> try:
... f.b
... except AttributeError, e:
... print e
...
Foo instance has no attribute 'b'
If you want to get a list of the attributes available on an object, try dir() or help()
>>> dir(f)
['__doc__', '__init__', '__module__', 'a']
>>> help(str)
Help on class str in module __builtin__:
class str(basestring)
| str(object) -> string
|
| Return a nice string representation of the object.
| If the argument is a string, the return value is the same object.
|
| Method resolution order:
| str
| basestring
| object
|
| Methods defined here:
|
| __add__(...)
| x.__add__(y) <==> x+y
|
[...]
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __new__ = <built-in method __new__ of type object>
| T.__new__(S, ...) -> a new object with type S, a subtype of T
You can even call help() on dir (why is left as an exercise for the reader):
>>> help(dir)
Help on built-in function dir in module __builtin__:
dir(...)
dir([object]) -> list of strings
If called without an argument, return the names in the current scope.
Else, return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it.
If the object supplies a method named __dir__, it will be used; otherwise
the default dir() logic is used and returns:
for a module object: the module's attributes.
for a class object: its attributes, and recursively the attributes
of its bases.
for any other object: its attributes, its class's attributes, and
recursively the attributes of its class's base classes.
Failing these... you could always look at the code, unless you've been provided some precompiled module by a third-party, in which case you should demand better documentation (say some unit tests!) from your supplier!
Your indentation is goofed, and you've mixed tabs and spaces. Run the script with python -tt to verify.
If you’re using python 3+ this may also occur if you’re using private variables that start with double underscore, e.g., self.__yourvariable. Just something to take note of for some of you who may run into this issue.
Here:
File "/Users/name/anaconda/lib/python3.6/http/client.py", line 71, in <module>
import email.parser
File "/Users/name/Desktop/Google Drive/FEBB/serverless/crwlr/email.py"
from bs4 import BeautifulSoup
The local email.py in /Users/name/Desktop/Google Drive/FEBB/serverless/crwlr/ shadows the stdlib's one. Now in your local email.py module, you're importing bs4, which imports html5lib, which imports xml.sax.saxutils, which imports urllib.request, which wants to import http.
IOW you end up with an (accidental) circular dependencie. At this point the http module is only partially imported, and doesn't yet have defined "client", hence the error.
The simple solution is to rename your "email.py" module to something else, or (if it's only a script and not a module) move it out of your pythonpath.
EDIT: I just noticed that your code started by importing http, so the http module should be already fully loaded, so even if the problem with your email.py script/module needs to be fixed, this shouldn't
lead to this problem. So chances are you have another http.py module or http package in your sys.path shadowing the stdlib's one. To debug this, add this line just after the import http one:
print(http)
This should print something like:
<module 'http' from '/some/path/to/a/python/file.pyc`>
If the path is not the one to your python install stdlib's "http/init.pyc" then you found the offender. If it's one of your own scripts/modules, the fix is the same as for email.py.
Might be Bs4 is raising the exception, Kindly execute the below script in the existing one validate Bs4 import is working fine
try:
from bs4 import BeautifulSoup
except Exception as err:
raise ImportError('Bs4 is not imported correctly. - {}'.format(err))