Rectangle 27 2

My guess would be that both attempts to create a driver - PhantomJS and FireFox - fail, meaning that driver is still None when you reach the finally block. You could confirm this by adding an explicit check:

finally:
        if driver:
            driver.close()
        else:
            print "Could not create driver"

As to why driver creation would be failing, the most likely explanation is an installation problem. You can test this by running a trivial example in your Python environment:

>>> from selenium.webdriver import Firefox
>>> d = Firefox()
>>> d.get('http://stackoverflow.com')

If this doesn't open Firefox and navigate to the front page of Stack Overflow, check the Selenium documentation for your OS.

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

python - AttributeError: 'NoneType' object has no attribute 'close' - ...

python selenium-webdriver phantomjs
Rectangle 27 4

Don't worry about the exception, you can safely ignore it. Your server has exited already, albeit a little messily.

None

You were shutting down the server with a Keyboard Interrupt (CTRL-C), triggering finalisation. In the meantime, the serve_forever thread was still running it's socket polling loop, but the select global in the SocketServer module had already been rebound. As a result the lookup for select.select() fails.

If this bothers you, upgrade to Python 3.4. This version no longer sets globals to None (in most cases), as per Safe Object Finalization; see PEP 442.

As for your Chrome error code and your other question, something is already bound to the port you are trying to use for Django. That's an entirely separate issue from the exception you saw for the select.select() call. It could be that another piece of software is holding on to that port, something that doesn't respond to HTTP requests (leading to the Chrome error response).

I followed the link you gave for determining what process is bound to a port, ran netstat but there was nothing on either port 8000 or 8888. I decided to try closing my VM and rebooting. This time when I ran runserver Chrome still says there is no response, but when I closed the server there was no error output from the console at all. I guess that's good if it shows the select error is resolved, but I have no idea where to go next with the 'no response' issue.

p.s. Maybe I should add/explain that 8888 is the port vagrant forwards to.

python - Error 324 empty response - AttributeError: 'NoneType' object ...

python django nonetype
Rectangle 27 1

AttributeError: 'NoneType' object has no attribute 'group'

I think the easiest way is to iterate over the list items searching for the match, and once found, get Group 1 contents and assign them to value:

import re
list = ['firstString','xxxSTATUS=100','thirdString','fourthString']
value = ""
for x in list:
    m = re.search('STATUS=(.*)', x)
    if m:
        value = m.group(1)
        break

print(value)

Note you do not need the initial .* in the pattern as re.search pattern is not anchored at the start of the string.

Also, if you want your initial approach to work, you need to check if there is a match first with if re.search('STATUS=(.*)', x), and then run it again to get the group contents with re.search('STATUS=(.*)', x).group(1):

value = next(re.search('STATUS=(.*)', x).group(1) for x in list if re.search('STATUS=(.*)', x))

python - using regex and capture groups in next method - Stack Overflo...

python regex
Rectangle 27 1

driver isn't initialised if both the creation of PhantomJS and Firefox fail.

Add a check before closing:

if driver:
    driver.close()

python - AttributeError: 'NoneType' object has no attribute 'close' - ...

python selenium-webdriver phantomjs
Rectangle 27 60

I have also seen this error when inadvertently naming a module with the same name as one of the standard Python modules. E.g. I had a module called commands which is also a Python library module. This proved to be difficult to track down as it worked correctly on my local development environment but failed with the specified error when running on Google App Engine.

I used abc.py to write a test to demonstrate the import behavior in python, that bites me a lot...

I suspected this and deleted the .py module but forgot to delete the .pyc which was still causing the error.

I created a math module , which is already standard module.

python - AttributeError: 'module' object has no attribute - Stack Over...

python attributeerror
Rectangle 27 60

I have also seen this error when inadvertently naming a module with the same name as one of the standard Python modules. E.g. I had a module called commands which is also a Python library module. This proved to be difficult to track down as it worked correctly on my local development environment but failed with the specified error when running on Google App Engine.

I used abc.py to write a test to demonstrate the import behavior in python, that bites me a lot...

I suspected this and deleted the .py module but forgot to delete the .pyc which was still causing the error.

I created a math module , which is already standard module.

python - AttributeError: 'module' object has no attribute - Stack Over...

python attributeerror
Rectangle 27 115

You have mutual top-level imports, which is almost always a bad idea.

If you really must have mutual imports in Python, the way to do it is to import them within a function:

# In b.py:
def cause_a_to_do_something():
    import a
    a.do_something()

Now a.py can safely do import b without causing problems.

(At first glance it might appear that cause_a_to_do_something() would be hugely inefficient because it does an import every time you call it, but in fact the import work only gets done the first time. The second and subsequent times you import a module, it's a quick operation.)

Thank you! I know for the first time that importing in different places make such a difference.

Please be aware that this adds over-head when the function is called, as you put the import logic at function call time, rather than program load time.

Interesting; I wonder why the interpreter does not give a proper error message in this case?

python - AttributeError: 'module' object has no attribute - Stack Over...

python attributeerror
Rectangle 27 114

You have mutual top-level imports, which is almost always a bad idea.

If you really must have mutual imports in Python, the way to do it is to import them within a function:

# In b.py:
def cause_a_to_do_something():
    import a
    a.do_something()

Now a.py can safely do import b without causing problems.

(At first glance it might appear that cause_a_to_do_something() would be hugely inefficient because it does an import every time you call it, but in fact the import work only gets done the first time. The second and subsequent times you import a module, it's a quick operation.)

Thank you! I know for the first time that importing in different places make such a difference.

Please be aware that this adds over-head when the function is called, as you put the import logic at function call time, rather than program load time.

Interesting; I wonder why the interpreter does not give a proper error message in this case?

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

python - AttributeError: 'module' object has no attribute - Stack Over...

python attributeerror
Rectangle 27 1

The error basically tells you that the figure does not have a canvas.

To prevent that you need to make sure to only start the animation once the figure is added to the FigureCanvasTkAgg. Since we lack the complete code in the question, I can only link you to some other complete examples that are reported to work:

In the second case the code from the question produces exactly the same error and a possible solution is to incorporate the animation in the tk class.

python - matplotlib.animation - AttributeError: 'NoneType' object has ...

python matplotlib tkinter
Rectangle 27 106

This works in Python 2.x.

import urllib.request
with urllib.request.urlopen("http://www.python.org") as url:
    s = url.read()
#I'm guessing this would output the html source code?
print(s)

Hi Eumiro, using the 'with' statement in Python I'm guessing it closes the connection automatically once it's done using it? Similar to a use statement in C#?

@Sergio: exactly! And through the indentation you see where your file is still opened.

Hello @eumiro, I have an error of "IndentationError: expected an indented block" when I type s = url.read(), may I ask how can I solve it please? x

s=url.read()

python - AttributeError: 'module' object has no attribute 'urlopen' - ...

python python-3.x urllib
Rectangle 27 31

The problem is the circular dependency between the modules. a imports b and b imports a. But one of them needs to be loaded first - in this case python ends up initializing module a before b and b.hi() doesn't exist yet when you try to access it in a.

Thank you! It is what I guessed. But I cannot find some documents mention it. If I do need two modules import some attributes from each other, what should I do?

@Stephen Hsu: Breaking circular dependencies is easy. It's already been asked on SO several times. stackoverflow.com/search?q=%5Bpython%5D+circular+dependency

@S.Lott: Thank you. I just know that it is a circular dependencies problem.

python - AttributeError: 'module' object has no attribute - Stack Over...

python attributeerror
Rectangle 27 30

The problem is the circular dependency between the modules. a imports b and b imports a. But one of them needs to be loaded first - in this case python ends up initializing module a before b and b.hi() doesn't exist yet when you try to access it in a.

Thank you! It is what I guessed. But I cannot find some documents mention it. If I do need two modules import some attributes from each other, what should I do?

@Stephen Hsu: Breaking circular dependencies is easy. It's already been asked on SO several times. stackoverflow.com/search?q=%5Bpython%5D+circular+dependency

@S.Lott: Thank you. I just know that it is a circular dependencies problem.

python - AttributeError: 'module' object has no attribute - Stack Over...

python attributeerror
Rectangle 27 3

>>> import tkinter as tk
>>> tk.canvas
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'canvas'
>>> tk.Canvas
<class 'tkinter.Canvas'>

python - Why do I keep getting AttributeError: 'module' object has no ...

python attributeerror
Rectangle 27 17

Your save_calendar view function isn't returning anything, which in Python is the same as returning None. The decorator tries to set an attribute on the returned response, and None can't be modified that way.

python - Why am I getting this error, "'NoneType' object has no attrib...

python django django-views decorator
Rectangle 27 13

I got this error by referencing an enum which was imported in a wrong way, e.g.:

from package import MyEnumClass
# ...
# in some method:
return MyEnumClass.Member
from package.MyEnumClass import MyEnumClass

Hope that helps someone

python - AttributeError: 'module' object has no attribute - Stack Over...

python attributeerror
Rectangle 27 13

I got this error by referencing an enum which was imported in a wrong way, e.g.:

from package import MyEnumClass
# ...
# in some method:
return MyEnumClass.Member
from package.MyEnumClass import MyEnumClass

Hope that helps someone

python - AttributeError: 'module' object has no attribute - Stack Over...

python attributeerror
Rectangle 27 1

The issue is not that the bundleWithPath call is returning something unexpected. As you said, None is a valid return value for it.

This suggests that the client's machine has a strange version of python in which

if None:
    print('hello, strange world')
else:
    print('hello, world')
hello strange world

I've never seen this myself, but it's the only explanation for what you are seeing, and there are a truckload of different python interpreters, so it's not shocking to me that one of them is a bit odd.

The commonly accepted practice for checking if a variable is None is to do so explicitly anyway -- it's a whole lot clearer to read, IMHO.

if mainBundle is not None:
    ...

The whole practice of coercing NoneType to False for boolean expressions leads to some weird and unclear semantics, especially when you have code that can produce True, False, or None.

I know this doesn't answer the "why" part of the question, but I'm chalking it up to a strange python version. It's hard to say more without having detailed information about the client's computer, or at least knowing the version of python being run.

thanks for your explanation - this makes sense (in a weird kind of way).

python - 'if' statement, not protecting against AttributeError: 'NoneT...

python objective-c macos pyobjc
Rectangle 27 329

You can use hasattr() or catch AttributeError, but if you really just want the value of the attribute with a default if it isn't there, the best option is just to use getattr():

getattr(a, 'property', 'default value')

This solves both aforementioned problems: a) The ambiguity of the source of a possible AttributeError, b) Preserving the EAFP approach.

It's also 25% of the lines of code. Surely this has to be the best solution.

This is the best solution "if you really just want the value of the attribute with a default." Although I believe this is what many people actually want when they say they want to detect whether an attribute is present, the OP actually asked for the latter, so it's reasonable for the direct answers to that question (hasattr, AttributeError) to be listed higher.

How to know if an object has an attribute in Python - Stack Overflow

python attributes
Rectangle 27 329

You can use hasattr() or catch AttributeError, but if you really just want the value of the attribute with a default if it isn't there, the best option is just to use getattr():

getattr(a, 'property', 'default value')

This solves both aforementioned problems: a) The ambiguity of the source of a possible AttributeError, b) Preserving the EAFP approach.

It's also 25% of the lines of code. Surely this has to be the best solution.

This is the best solution "if you really just want the value of the attribute with a default." Although I believe this is what many people actually want when they say they want to detect whether an attribute is present, the OP actually asked for the latter, so it's reasonable for the direct answers to that question (hasattr, AttributeError) to be listed higher.

How to know if an object has an attribute in Python - Stack Overflow

python attributes