You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Microsoft Azure上传Python脚本?及Zip上传报错与代码排查

Answers to Your Azure & Python Script Issues

Hey there, let's tackle your two questions step by step—first how to upload Python scripts to Azure, then fixing that "directory not found" error with your zip file.

1. Uploading Python Scripts to Microsoft Azure

If you're working with Azure Machine Learning (AML) (which it looks like you are, given your code), here are two reliable methods:

Via Azure Machine Learning Studio UI

  • Log into your AML workspace in the AML Studio interface
  • Navigate to Assets > Scripts from the left sidebar
  • Click Create > Script, then select your local Python script file
  • Fill in a name and optional description, then hit Create to finish uploading
  • If you're building a pipeline, you can also upload scripts directly when creating pipeline components

Via Azure Python SDK

For programmatic uploads (great for automation), use the AML SDK:

from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential
from azure.ai.ml.entities import ScriptPackage

# Initialize the ML client with your workspace details
ml_client = MLClient(
    DefaultAzureCredential(),
    subscription_id="your-subscription-id",
    resource_group_name="your-resource-group",
    workspace_name="your-workspace-name"
)

# Upload your local script directory
script = ScriptPackage(
    name="my-prediction-script",
    version="1.0",
    path="./path-to-your-local-script-folder/"
)
ml_client.create_or_update(script)

2. Fixing the "Directory Not Found" Error with Your Zip File

The core issue here is hardcoded local Windows paths in your code—Azure's cloud environment doesn't have access to your C:\Users\... directory. Let's fix this and adjust your zip structure:

Step 1: Fix Your Zip File Structure

  • Move your model_2019_08_09_12_56_55_756.pkl file into the same folder as your Python script (let's call this folder model)
  • Zip the entire model folder, not just the files inside. This ensures that when Azure unzips it, the script and model file are in the same directory structure.

Step 2: Rewrite Your Code to Use Relative Paths

Replace those absolute local paths with relative paths that work in Azure's environment. Here's the corrected code:

import pandas as pd
import pickle
import os

def azureml_main(dataframe1 = None, dataframe2 = None):
    # Get the directory where your script is running
    script_dir = os.path.dirname(os.path.abspath(__file__))
    # Build the path to your model file relative to the script
    model_path = os.path.join(script_dir, "model_2019_08_09_12_56_55_756.pkl")
    
    # Load the model using the relative path
    model = pickle.load(open(model_path, 'rb'))
    pred = model.predict(dataframe1)
    return pd.DataFrame([pred[0]])

If your script and model file are in the exact same folder, you can even simplify it to just model = pickle.load(open("model_2019_08_09_12_56_55_756.pkl", 'rb'))—no need for the os.path stuff.

Step 3: Upload & Test the Zip Correctly

  • Upload your revised zip file to AML Studio (either as a Script or Data asset, depending on how you're using it)
  • When running the script in Azure, the platform will unzip the file into the working directory of the compute instance/container. Your relative paths will now correctly point to the model file.

Extra Tips to Avoid Future Issues

  • Make sure the Python version in your Azure compute environment matches the version you used to generate the .pkl file (pickle can break across versions)
  • If you're using an AML Compute Instance, after uploading the zip, you can open the terminal to check the unzipped file structure and verify paths manually

内容的提问来源于stack exchange,提问作者sahil aseeja

火山引擎 最新活动