I'm very new to Python and am learning how packages and modules work but have run into a snag. Initially I created a very simple package to be deployed as a Lambda function. I have a file in the root directory called lambda.py which contains a handler function, and I put most of my business logic in a separate file. I created a subdirectory - for this example let's say it's called testme - and more specifically put it in __init__.py underneath that subdirectory. Initially this worked great. Inside my lambda.py file I could use this import statement:
from testme import TestThing # TestThing is the name of the class However, now that the code is growing, I've been splitting things into multiple files. As a result, my code no longer runs; I get the following error:
TypeError: 'module' object is not callable Here's a simplified version of what my code looks like now, to illustrate the problem. What am I missing? What can I do to make these modules "callable"?
/lambda.py:
#!/usr/bin/env python2 # -*- coding: utf-8 -*- from testme import TestThing def handler(event, context): abc = TestThing(event.get('value')) abc.show_value() if __name__ == '__main__': handler({'value': 5}, None) /testme/__init__.py:
#!/usr/bin/env python2 # -*- coding: utf-8 -*- __project__ = "testme" __version__ = "0.1.0" __description__ = "Test MCVE" __url__ = "https://stackoverflow.com" __author__ = "soapergem" __all__ = ["TestThing"] /testme/TestThing.py:
#!/usr/bin/env python2 # -*- coding: utf-8 -*- class TestThing: def __init__(self, value): self.value = value def show_value(self): print 'The value is %s' % self.value Like I said, the reason I'm doing all this is because the real world example has enough code that I want to split it into multiple files inside the subdirectory. And so I left an __init__.py file there to just to serve as an index essentially. But I am very unsure of what the best practices are for package structure, or how to get this working.