'Import a python property in other file python

py like this

class MyStack(core.Stack):
    def __init__(self, scope: cdk.Construct, construct_id: str,  **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        my_api = _apigateway.LambdaRestApi(
            scope=self,
            proxy=False
        )

I want to get this my_api property in 2nd file I am trying something like this

from .file1 import my_api

but getting error of ImportError: cannot import name



Solution 1:[1]

You define a class here. Then in your other module you will want to import the module containing the class, and instanciate it.

import ma_lib
[some code that creates a my_app object that is CDK app]
my_stack = MyStack(my_app, "Name")
my_api = my_stack.my_api

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1