Manage Python 3 packages in Azure Automation
Ez a cikk bemutatja, hogyan importálhatja, kezelheti és használhatja a Python 3-csomagokat az Azure Automationben, amely az Azure-tesztkörnyezetben és a hibrid Runbook-feldolgozókban fut. Python packages should be downloaded on Hybrid Runbook workers for successful job execution. To help simplify runbooks, you can use Python packages to import the modules you need.
A Python 2-csomagok kezelésével kapcsolatos információkért lásd: Python 2-csomagok kezelése.
Alapértelmezett Python-csomagok
To support Python 3.8 runbooks in the Automation service, some Python packages are installed by default and a list of these packages are here. The default version can be overridden by importing Python packages into your Automation account.
Preference is given to the imported version in your Automation account. Egyetlen csomag importálásához lásd : Csomag importálása. Ha több csomagot tartalmazó csomagot szeretne importálni, olvassa el a függőségekkel rendelkező csomag importálása című témakört.
Megjegyzés:
A Python 3.10-hez (előzetes verzió) nincsenek telepítve alapértelmezett csomagok.
Csomagok forrásfájlokként
Az Azure Automation csak olyan Python-csomagot támogat, amely csak Python-kódot tartalmaz, és nem tartalmaz más nyelvi bővítményeket vagy kódot más nyelveken. Előfordulhat azonban, hogy az Azure Sandbox-környezet nem rendelkezik a C/C++ bináris fájlokhoz szükséges fordítókkal, ezért javasoljuk, hogy inkább kerékfájlokat használjon.
Megjegyzés:
A Python 3.10 (előzetes verzió) jelenleg csak a kerekes fájlokat támogatja.
A Python-csomagindex (PyPI) a Python programozási nyelv szoftvereinek adattára. Amikor kiválaszt egy Python 3-csomagot, amely importálható az Automation-fiókjába a PyPI-ból, jegyezze fel a következő fájlnévrészeket:
Válasszon egy Python-verziót:
Fájlnév rész | Leírás |
---|---|
cp38 | Az Automation támogatja a Python 3.8 for Cloud-feladatokat. |
amd64 | Az Azure-tesztkörnyezeti folyamatok Windows 64 bites architektúrák. |
Például:
- Pandas importálásához válasszon ki egy hasonló nevű
pandas-1.2.3-cp38-win_amd64.whl
kerekes fájlt.
A PyPI-n elérhető Egyes Python-csomagok nem biztosítanak kerékfájlt. Ebben az esetben töltse le a forrást (.zip vagy .tar.gz fájl), és hozza létre a kerékfájlt a .pip
Hajtsa végre a következő lépéseket egy 64 bites Windows rendszerű géppel, amelyen telepítve van a Python 3.8.x és a kerékcsomag:
- Töltse le a forrásfájlt
pandas-1.2.4.tar.gz
. - Futtassa a pipet a kerékfájl lekéréséhez a következő paranccsal:
pip wheel --no-deps pandas-1.2.4.tar.gz
Csomag importálása
In your Automation account, select Python packages under Shared Resources. Then select + Add a Python package.
On the Add Python Package page, select a local package to upload. The package can be .whl or .tar.gz file for Python 3.8 and .whl file for Python 3.10 (preview).
Enter a name and select the Runtime Version as Python 3.8 or Python 3.10 (preview).
Megjegyzés:
A Python 3.10 -es (előzetes verziójú) futtatókörnyezet verziója jelenleg a felhőbeli és a hibrid feladatok esetében is támogatott az összes nyilvános régióban, kivéve Ausztrália középső régiójában, Dél-Koreában, Dél-Svédországban, Jio India középső régiójában, Délkelet-Brazíliában, Közép-Indiában, Nyugat-Indiában, az Egyesült Arab Emírségek középső régiójában és a Gov-felhőkben.
Select Import.
After a package has been imported, it's listed on the Python packages page in your Automation account. To remove a package, select the package and select Delete.
Függőségeket tartalmazó csomag importálása
You can import a Python 3.8 package and its dependencies by importing the following Python script into a Python 3.8 runbook. Ensure that Managed identity is enabled for your Automation account and has Automation Contributor access for successful import of package.
https://github.com/azureautomation/runbooks/blob/master/Utility/Python/import_py3package_from_pypi.py
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.
Megjegyzés:
A Python 3.10 (előzetes verzió) esetében jelenleg nem támogatott runbook importálása az Azure Portalról.
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.7. If it does, make sure to change it to Python 3.8.
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.
A szkripthez (import_py3package_from_pypi.py
) a következő paraméterek szükségesek.
Parameter | Description |
---|---|
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 | A modul neve, amelyből importálni szeretné pypi.org |
module_version | Version of the module |
Parameter value should be provided as a single string in the below format:
-s <subscription_id> -g <resource_group> -a<automation_account> -m <module_name> -v <module_version>
A paraméterek runbookokkal való használatáról további információt a Runbook paramétereinek használata című témakörben talál.
Csomag használata runbookban
Az importált csomag egy runbookban is használható. Adja hozzá az alábbi kódot egy Azure-előfizetés összes erőforráscsoportjának listázásához.
#!/usr/bin/env python3
import os
import requests
# printing environment variables
endPoint = os.getenv('IDENTITY_ENDPOINT')+"?resource=https://management.azure.com/"
identityHeader = os.getenv('IDENTITY_HEADER')
payload={}
headers = {
'X-IDENTITY-HEADER': identityHeader,
'Metadata': 'True'
}
response = requests.request("GET", endPoint, headers=headers, data=payload)
print(response.text)
Megjegyzés:
A Python-csomag automationassets
nem érhető el pypi.org, ezért nem importálható hibrid Windows-runbook-feldolgozóba.
A tesztkörnyezetben elérhető csomagok azonosítása
Az alapértelmezett telepített modulok felsorolásához használja az alábbi kódot:
#!/usr/bin/env python3
import pkg_resources
installed_packages = pkg_resources.working_set
installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
for i in installed_packages])
for package in installed_packages_list:
print(package)
Python 3.8 PowerShell-parancsmagok
Új Python 3.8-csomag hozzáadása
New-AzAutomationPython3Package -AutomationAccountName tarademo -ResourceGroupName mahja -Name requires.io -ContentLinkUri https://files.pythonhosted.org/packages/7f/e2/85dfb9f7364cbd7a9213caea0e91fc948da3c912a2b222a3e43bc9cc6432/requires.io-0.2.6-py2.py3-none-any.whl
Response
ResourceGroupName : mahja
AutomationAccountName : tarademo
Name : requires.io
IsGlobal : False
Version :
SizeInBytes : 0
ActivityCount : 0
CreationTime : 9/26/2022 1:37:13 PM +05:30
LastModifiedTime : 9/26/2022 1:37:13 PM +05:30
ProvisioningState : Creating
Az összes Python 3.8-csomag listázása
Get-AzAutomationPython3Package -AutomationAccountName tarademo -ResourceGroupName mahja
Response :
ResourceGroupName : mahja
AutomationAccountName : tarademo
Name : cryptography
IsGlobal : False
Version :
SizeInBytes : 0
ActivityCount : 0
CreationTime : 9/26/2022 11:52:28 AM +05:30
LastModifiedTime : 9/26/2022 12:11:00 PM +05:30
ProvisioningState : Failed
ResourceGroupName : mahja
AutomationAccountName : tarademo
Name : requires.io
IsGlobal : False
Version :
SizeInBytes : 0
ActivityCount : 0
CreationTime : 9/26/2022 1:37:13 PM +05:30
LastModifiedTime : 9/26/2022 1:39:04 PM +05:30
ProvisioningState : ContentValidated
ResourceGroupName : mahja
AutomationAccountName : tarademo
Name : sockets
IsGlobal : False
Version : 1.0.0
SizeInBytes : 4495
ActivityCount : 0
CreationTime : 9/20/2022 12:46:28 PM +05:30
LastModifiedTime : 9/22/2022 5:03:42 PM +05:30
ProvisioningState : Succeeded
Az adott csomag részleteinek lekérése
Get-AzAutomationPython3Package -AutomationAccountName tarademo -ResourceGroupName mahja -Name sockets
Response
ResourceGroupName : mahja
AutomationAccountName : tarademo
Name : sockets
IsGlobal : False
Version : 1.0.0
SizeInBytes : 4495
ActivityCount : 0
CreationTime : 9/20/2022 12:46:28 PM +05:30
LastModifiedTime : 9/22/2022 5:03:42 PM +05:30
ProvisioningState : Succeeded
Python 3.8-csomag eltávolítása
Remove-AzAutomationPython3Package -AutomationAccountName tarademo -ResourceGroupName mahja -Name sockets
Python 3.8-csomag frissítése
Set-AzAutomationPython3Package -AutomationAccountName tarademo -ResourceGroupName mahja -Name requires.io -ContentLinkUri https://files.pythonhosted.org/packages/7f/e2/85dfb9f7364cbd7a9213caea0e91fc948da3c912a2b222a3e43bc9cc6432/requires.io-0.2.6-py2.py3-none-any.whl
ResourceGroupName : mahja
AutomationAccountName : tarademo
Name : requires.io
IsGlobal : False
Version : 0.2.6
SizeInBytes : 10109
ActivityCount : 0
CreationTime : 9/26/2022 1:37:13 PM +05:30
LastModifiedTime : 9/26/2022 1:43:12 PM +05:30
ProvisioningState : Creating
Következő lépések
Python-runbook előkészítéséhez lásd : Python-runbook létrehozása.