1

I'm developing a multi-level package in python:

/mega_package
    __init__.py
    a.py
    /medium_package
        __init__.py
        /internal_package
            __init__.py
            exceptions.py

Inside the exceptions.py module I have a custom Exception:

class VeryBadException(Exception):
    pass

I import this Exception from the module a.py in the mega package in the following way:

from medium_package.internal_package import VeryBadException

And I can catch it when running a main script in the context of the mega package.

When I import the mega_package as a plugin to an external module, I must import this exception in the following way:

from mega_package.medium_package.internal_package import VeryBadException

My problem is that if the one of the modules in the mega_package raise the VeryBadException I can't catch it.

My code looks like that:

import mega_package
try:
    mega_package.a.do_bad_thing()
except VeryBadException:
    print 'Got it!!!'
except Exception,e:
    print type(e)
    print type(VeryBadException())

and the output is

<class 'medium_package.internal_package.exceptions.VeryBadExcpetion'>
<class 'mega_package.medium_package.internal_package.exceptions.VeryBadExcpetion'>

How can I resolve this in an elegant way?

bobibanai
  • 11
  • 1
  • Did you try using `from .medium_package.internal_package import VeryBadException` inside `a` (note extra dot at the beginning)? – BrenBarn Jan 16 '18 at 07:56

0 Answers0