Manage Python 3 packages (preview) in Azure Automation
Azure Automation allows you to run Python 3 runbooks (preview) on Azure Sandbox environment and on Linux Hybrid Runbook Workers. To help in simplification of runbooks, you can use Python packages to import the modules that you need. To import a single package, see Import a package. To import a package with multiple packages, see Import a package with dependencies. This article describes how to manage and use Python 3 packages (preview) in Azure Automation.
Packages as source files
Azure Automation supports only a Python package that only contains Python code and doesn't include other language extensions or code in other languages. However, the Azure Sandbox environment might not have the required compilers for C/C++ binaries, so it's recommended to use wheel files instead. The Python Package Index (PyPI) is a repository of software for the Python programming language. When selecting a Python 3 package to import into your Automation account from PyPI, note the following filename parts:
|cp38||Automation supports Python 3.8.x for Cloud Jobs.|
|amd64||Azure sandbox processes are Windows 64-bit architecture.|
For example, if you wanted to import pandas, you could select a wheel file with a name similar as
Some Python packages available on PyPI don't provide a wheel file. In this case, download the source (.zip or .tar.gz file) and generate the wheel file using
pip. For example, perform the following steps using a 64-bit machine with Python 3.8.x and wheel package installed:
- Download the source file
- Run pip to get the wheel file with the following command:
pip wheel --no-deps pandas-1.2.4.tar.gz.
Import a package
In your Automation account, select Python packages under Shared Resources. Then select + Add a Python package.
On the Add Python Package page, select Python 3 for the Version, and select a local package to upload. The package can be a .whl or .tar.gz file. When the package is selected, select OK to upload it.
Once a package has been imported, it's listed on the Python packages page in your Automation account, under the Python 3 packages (preview) tab. If you need to remove a package, select the package and select Delete.
Import a package with dependencies
You can import a Python 3 package and its dependencies by importing the following Python script into a Python 3 runbook, and then running it.
Importing the script into a runbook
For information on importing the runbook, see Import a runbook from the Azure portal. Copy the file from GitHub to storage that the portal can access before you run the import.
The Import a runbook page defaults the runbook name to match the name of the script. If you have access to the field, you can change the name. Runbook type may default to Python 2. If it does, make sure to change it to Python 3.
Executing the runbook to import the package and dependencies
After creating and publishing the runbook, run it to import the package. See Start a runbook in Azure Automation for details on executing the runbook.
The script (
import_py3package_from_pypi.py) requires the following parameters.
|subscription_id||Subscription ID of the Automation account|
|resource_group||Name of the resource group that the Automation account is defined in|
|automation_account||Automation account name|
|module_name||Name of the module to import from
For more information on using parameters with runbooks, see Work with runbook parameters.
Use a package in a runbook
With the package imported, you can use it in a runbook. Add the following code to list all the resource groups in an Azure subscription.
import os import azure.mgmt.resource import automationassets def get_automation_runas_credential(runas_connection): from OpenSSL import crypto import binascii from msrestazure import azure_active_directory import adal # Get the Azure Automation RunAs service principal certificate cert = automationassets.get_automation_certificate("AzureRunAsCertificate") pks12_cert = crypto.load_pkcs12(cert) pem_pkey = crypto.dump_privatekey(crypto.FILETYPE_PEM,pks12_cert.get_privatekey()) # Get run as connection information for the Azure Automation service principal application_id = runas_connection["ApplicationId"] thumbprint = runas_connection["CertificateThumbprint"] tenant_id = runas_connection["TenantId"] # Authenticate with service principal certificate resource ="https://management.core.windows.net/" authority_url = ("https://login.microsoftonline.com/"+tenant_id) context = adal.AuthenticationContext(authority_url) return azure_active_directory.AdalAuthentication( lambda: context.acquire_token_with_client_certificate( resource, application_id, pem_pkey, thumbprint) ) # Authenticate to Azure using the Azure Automation RunAs service principal runas_connection = automationassets.get_automation_connection("AzureRunAsConnection") azure_credential = get_automation_runas_credential(runas_connection) # Intialize the resource management client with the RunAs credential and subscription resource_client = azure.mgmt.resource.ResourceManagementClient( azure_credential, str(runas_connection["SubscriptionId"])) # Get list of resource groups and print them out groups = resource_client.resource_groups.list() for group in groups: print(group.name)
automationassets package is not available on pypi.org, so it's not available for import onto a Windows machine.
To prepare a Python runbook, see Create a Python runbook.