0

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.

1 Answer 1

3

You either have to import your class in your __init__ file:

testme/__init__.py:

from .TestThing import TestThing 

or import it using the full path:

lambda.py:

from testme.TestThing import TestThing 

When you use a __init__.py file, you create a package, and this package (named after the root directory, e.g. testme, may include submodules. Those are accessible via the package.module syntax, but the contents of the submodules are only visible in the root package if you explicitly import them there.

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

2 Comments

Is either one more common? Or considered best practice?
Depends on your usecase. In large projects like numpy or scipy you find both: Common objects are visible in the root package, others only in subpackages, other submodules have to be imported explicitly by the user.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.