Azure Automation runbook with multiple Python classes in separate files

Antti-Jussi Korjonen 25 Reputation points
2024-09-03T07:27:43.4566667+00:00

I am developing multiple Python classes (in separate files) to be used in a runbook using Azure DevOps (git) as version control with runbooks automatically syncing to Azure Automation.

I know there is a way to include the modules in the environment but I don't want to do that.Not yet, because I'm still developing those classes.

I could also pull the class files from DevOps separately but I do not want to that either.

Is there any way to include all the files the runbook needs in the runbook run?

My workaround currently is to concatenate all the files in one file, which works but I don't like it either.

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,366 questions
0 comments No comments
{count} votes

Accepted answer
  1. Amira Bedhiafi 33,071 Reputation points Volunteer Moderator
    2025-05-27T18:38:04.0333333+00:00

    Hello !

    Thank you for posting on Microsoft Learn.

    IMHO, your current approach might work within Azure Automation and you will find no dependency management required but it is kind of messy for DEV and version control and hard to debug or unit test modularly.

    You can try to bundle your runbook and modules into a .zip file and upload it via Azure Automation or through Azure CLI. Inside the runbook, you can then use:

    import sys
    sys.path.append('/path/to/your/modules')
    

    But, Azure Automation doesn't allow you to define a custom path directly in cloud execution so this is only practical if uploaded to a custom worker (for example Hybrid Worker).

    You can also, set up a Hybrid Worker on a VM where you control the environment. There, your Python environment can:

    • Use relative imports
    • Load from folders
    • Pull from DevOps
    • Work like normal Python

    But if you're strictly using Azure cloud workers and want to keep modularity:

    Convert your .py files to base64 strings.

    Decode and exec() them dynamically in the main runbook.

    import base64  code = base64.b64decode(b'...base64encodedcode...') exec(code)
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.