首页 > 解决方案 > 在 Azure Batch 上运行 python 脚本

问题描述

我正在尝试在作为 linux dsvm 的 azure batch 上执行 python 脚本,以便该脚本可以安装 python 包,然后执行 python 脚本。

以下是我使用的代码:

try:
   from pip import main as pipmain
except ImportError:
   from pip._internal import main as pipmain

try:
    import pandas as pd
except:

   pipmain(['install', 'pandas',"])

import pandas

当我在 azure Batch 命令行上运行 python 脚本时,池任务在最后一条语句(导入 pandas)处出错,尽管我可以在标准输出日志文件中看到安装了 pandas、numpy 等包。

似乎这些软件包安装在其他位置,并且在尝试导入时尝试从其他位置导入。它给出了错误 ImportError: No module named pandas in the stderr.txt file on the azure batch pool tasks。

我试图安装 python 包并将其导入相同脚本的原因是因为天蓝色批处理命令行不允许我编写 2 个命令,例如

pip install pandas
python test.py

它首先安装软件包,然后调用它只是导入熊猫库的脚本。

我还在批处理池的启动任务中使用了该pip install pandas命令pip install --install-option="--prefix=$AZ_BATCH_TASK_WORKING_DIR" pandas。AZ_BATCH_TASK_WORKING_DIR 据我了解是任务批处理运行时任务和脚本可以访问的工作目录

有没有办法在 Azure Batch 上成功运行 python 脚本。在moemt iam 只运行一个命令:import pandas

标签: pythonazure-data-factoryazure-batch

解决方案


您需要提供内联 shell 脚本来运行多个命令并利用 shell 扩展。请参阅此文档。您需要运行两个命令,例如:

/bin/bash -c "pip install pandas && python test.py"

此外,任务在特定于上下文的目录下运行(即,启动任务在启动任务目录中运行,而普通任务将在不同的目录中运行,尽管$AZ_BATCH_TASK_WORKING_DIR名称相同)并且用户身份也可以修改用户上下文一个任务正在运行。


推荐阅读