Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

global name 'ParseError' is not defined, I used try and except to avoid it but this still shows up

Tags:

python

django

it's occurring from the line with except ParseError which I don't understand, because I wrote this line to avoid this error. Here's my code

import json
from goose import Goose

DEFAULT = 'https://images.unsplash.com/photo-1427435150519-42d9bcd0aa81?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=10d1cfc208c8ed5ed1160e851eabce1d'
def extract(url):
    g = Goose()
    try:
        article = g.extract(url=url)
        if article.top_image is None:
            return DEFAULT 

        else:
            if article.top_image.src is None:
              return DEFAULT
            else:
                resposne = {'image':article.top_image.src}
                return article.top_image.src
    except ParseError:
        if can_handle():
                handle_exception()
            else:
                print("couldn't handle exception: url={0}".format(url))
                raise

I mainly just want to avoid this error, if I get this error I want just default image to show up. What am I missing here?

Here;s my traceback

Traceback:
File "/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
  71.             return self.dispatch(request, *args, **kwargs)
File "/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
  34.             return bound_func(*args, **kwargs)
File "/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "/local/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
  30.                 return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/younggue/Desktop/ebagu0.2/rclone/main/views.py" in dispatch
  172.      return super(PostCreateView, self).dispatch(request, *args, **kwargs)       
File "/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
  89.         return handler(request, *args, **kwargs)
File "/local/lib/python2.7/site-packages/django/views/generic/edit.py" in post
  249.         return super(BaseCreateView, self).post(request, *args, **kwargs)
File "/local/lib/python2.7/site-packages/django/views/generic/edit.py" in post
  215.             return self.form_valid(form)
File "main/views.py" in form_valid
  165.          self.object.image = extract(self.object.url) 
File "main/util/media.py" in extract
  18.   except ParseError:

Exception Type: NameError at /add_post/
Exception Value: global name 'ParseError' is not defined
like image 283
winixxee Avatar asked Feb 15 '16 06:02

winixxee


1 Answers

I got this same error when pandas to load files from a list of files. Following @Sayse's comment, after running the code, I found lib/site-packages/pandas/io/parsers.py in the stack trace.

To correct the problem, you must import that error from the file in which it is define. In my case, I used

import pandas as pd
#~~~~~~~~~~ Solution: import the error ~~~~~~~~~~
from pandas.io.parsers import ParserError
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...
for fname in fnames:
    try:
        df = pd.read_csv(fname)
    except ParserError:
        logger.info(f'Skipping incompatible file: {fname}')
like image 113
Steven C. Howell Avatar answered Nov 14 '22 22:11

Steven C. Howell