Himanshu Mishra Himanshu Mishra - 5 months ago 12
Python Question

import types in python

I have a module named

types
which has the following code in it

def Hello():
return "Hello World"


But when I do

>>> import types
>>> types.Hello()


It gives me

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Hello'


This is really freaking me out.

Answer

types is a built-in module and import searches for a built-in module first before looking into local package.

>>> import types
>>> types
<module 'types' from '/usr/lib/python2.7/types.pyc'>

Try

>>> from . import types
Comments