0

I have three files in folder

exceptions.py

class MyException(Exception):
    pass

MyClass.py which begins as:

from exceptions import MyException

And empty __init__.py

When I'm trying to import MyClass.py there is Error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "MyClass.py", line 1, in <module>
    from exceptions import MyException
ImportError: cannot import name MyException

I've read docs and a lot of articles but can't find what's wrong

2
  • is it empty init.py or init.py? Commented Aug 26, 2015 at 13:47
  • Do import exceptions and have a look what dir(exceptions) has to say. Commented Aug 26, 2015 at 13:49

2 Answers 2

1

There is a standard built-in module named exceptions which is imported instead of your exceptions.py.

You can either rename your exceptions.py or use dotted import: from .exceptions import MyException

See https://docs.python.org/2/tutorial/modules.html#intra-package-references for more.

Sign up to request clarification or add additional context in comments.

Comments

1

You can't use the name exeptions.py as there already exists a module named exeptions, which has no class named Myexeption, so you are getting thus error. Just change the file name and you will be all right.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.