

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 启动 AgentCore 代码解释器会话
<a name="code-interpreter-start-session"></a>

创建代码解释器后，您可以启动会话来执行代码。

**Example**  

1. 要使用 Amazon CLI 启动代码解释器会话，请使用以下`start-code-interpreter-session`命令：

   ```
   aws bedrock-agentcore start-code-interpreter-session \
     --region <Region> \
     --code-interpreter-id "<your-code-interpreter-id>" \
     --name "my-code-session" \
     --description "My Code Interpreter session for data analysis" \
     --session-timeout-seconds 900
   ```

1. 要使用 Amazon 适用于 Python 的软件开发工具包启动代码解释器会话，请使用以下`start_code_interpreter_session`方法：

   ```
   import boto3
   
   # Initialize the boto3 client
   dp_client = boto3.client(
       'bedrock-agentcore',
       region_name="<Region>",
       endpoint_url="https://bedrock-agentcore.<Region>.amazonaws.com"
   )
   
   # Start a Code Interpreter session
   response = dp_client.start_code_interpreter_session(
       codeInterpreterIdentifier="aws.codeinterpreter.v1",
       name="sandbox-session-1",
       sessionTimeoutSeconds=3600
   )
   
   # Print the session ID
   session_id = response["sessionId"]
   print(f"Session created: {session_id}")
   ```

1. 要使用 API 启动新的代码解释器会话，请使用以下调用：

   ```
   # Using awscurl
   awscurl -X PUT \
     "https://bedrock-agentcore.<Region>.amazonaws.com/code-interpreters/aws.codeinterpreter.v1/sessions/start" \
     -H "Content-Type: application/json" \
     -H "Accept: application/json" \
     --service bedrock-agentcore \
     --region <Region> \
     -d '{
       "name": "code-session-abc12345",
       "description": "code sandbox session",
       "sessionTimeoutSeconds": 900
     }'
   ```
**注意**  
您可以使用托管资源 ID `aws.codeinterpreter.v1` 或通过创建代码解释器获得的资源 ID `CreateCodeInterpreter`。