Azazell00
Azazell00

Reputation: 93

Error with Import in python from the same folder

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

Upvotes: 0

Views: 168

Answers (2)

Maksym Polshcha
Maksym Polshcha

Reputation: 18368

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.

Upvotes: 1

Ahasanul Haque
Ahasanul Haque

Reputation: 11164

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.

Upvotes: 1

Related Questions