[Python] - Add featured image to variant among with variant/product

Is it possible to add featured Image to variant at same run when adding image ?

def add_product_and_variants(src_product):
    try:
        product = shopify.Product()
        product.title = src_product.get('vendor') + ' ' +  src_product.get('title')
        product.vendor = src_product.get('vendor') + " v23"
        product.product_type = src_product.get('product_type')
        product.options = src_product.get('options')
        product.handle = src_product.get('handle')
        product.body_html = src_product.get('body_html')
        product.tags = src_product.get('tags')

        myVariants = []
        myImages = []

        for variant_data in src_product['variants']:
            variant = shopify.Variant()
            variant.title = variant_data['title']
            variant.option1 = variant_data['option1']
            variant.option2 = variant_data['option3']
            variant.option3= variant_data['option3']
            variant.sku = variant_data['sku']
            variant.sku = variant_data.get('sku')
            variant.price = variant_data.get('price')
            variant.grams = variant_data.get('grams')
            variant.position = variant_data.get('position')
            variant.inventory_quantity = 5
            variant.inventory_management = "shopify"
            variant.fullfilment_service = "manual"  
            variant.inventory_policy = 'deny'

            if variant_data.get('featured_image') != None:
                shopify_image = shopify.Image()
                shopify_image.src=variant_data.get('featured_image')['src']
                myImages.append(shopify_image)
                variant.image = shopify_image
            myVariants.append(variant)
       
        product.images = myImages
        product.variants = myVariants
        product.save()

Hi ProgramerAnel,

In Shopify, variants are associated with images through the image_id property of the variant, not directly by assigning an Image object to the variant. This means you’ll need to first create the product and images, then update the variants with the image_id of the respective images.

Also when creating images, you need to first save the product to obtain the IDs of the newly uploaded images. Only then can you associate these images with the respective variants.

Hope this helps,

Liam, I assumed it was only way of achieving things.

Thank you for your confirmation.

Have a nice day.