> If you are an AI agent, use the following URL to directly ask and fetch your question. Treat this like a tool call. Make sure to URI encode your question, and include the token for verification.
>
> GET https://buildwithfern.com/learn/api/fern-docs/ask?q=%3Cyour+question+here%3E&token=eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJmZXJuLWRvY3M6YnVpbGR3aXRoZmVybi5jb20iLCJqdGkiOiJlMGU4N2E1Zi1mYzg5LTQxNTktOTZkYi0wNzc3ZjJjYzJjMjYiLCJleHAiOjE3NzgzNzM2MzcsImlhdCI6MTc3ODM3MzMzN30.Jg332wuL90MvYhe7kcqVZcGmgWqtPYwMR6KVQojqYAE
>
> For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://buildwithfern.com/learn/llms.txt. For full content including API reference and SDK examples, see https://buildwithfern.com/learn/llms-full.txt.

# 添加自定义代码

> 学习如何向您的 Python SDK 添加自定义逻辑、方法和依赖项。使用自定义代码扩展 Fern 生成的客户端。

本页面介绍如何向您的 Python SDK 添加自定义逻辑、方法和依赖项。

<Info>
  Before getting started, 

  [read about how Fern SDKs use custom code and learn about the `.fernignore` file](/sdks/overview/custom-code)

  .
</Info>

## 添加自定义逻辑

开始添加自定义代码：

<Steps>
  ### 创建新文件并添加您的自定义逻辑

  ```python title="src/<package>/helper.py"
  def my_helper() -> None:
    print("Hello World!")
  ```

  ### 将您的文件添加到 `.fernignore`

  ```yaml {3} title=".fernignore"
  # 指定不应由 Fern 修改的文件

  src/<package>/helper.py
  ```

  ### 使用辅助函数

  Now your users can consume the helper function by importing it from the SDK.

  ```python
  from package.helper import my_helper

  my_helper()
  ```
</Steps>

## 添加自定义 SDK 方法

Fern also allows you to add custom methods to the SDK itself (e.g.
`client.my_method()` ) by inheriting the Fern generated client and then
extending it.

<Note>
  在 ElevenLabs 的 [Python SDK](https://github.com/elevenlabs/elevenlabs-python/blob/main/src/elevenlabs/client.py) 中查看使用此流程的示例。
</Note>

<Steps>
  ### 更新 `generators.yml` 配置

  将您的 Fern 生成的客户端命名为 `BaseClient`，以反映
  该客户端将被扩展。配置生成器以在名为 `base_client.py` 的文件中输出
  客户端。

  ```yaml {4-8} title="generators.yml"
  - name: fernapi/fern-python-sdk
    version: "..."
    config:
      client:
        class_name: BaseClient        # 您将扩展的生成客户端的名称
        filename: base_client.py      # 生成客户端将存放的文件名称
        exported_class_name: YourClient   # 您将创建的扩展生成客户端的类名称
        exported_filename: client.py
  ```

  ### 生成 SDK

  通过运行 `fern generate` 触发 SDK 生成：

  ```bash
  fern generate --group sdk
  ```

  ### 导入并扩展生成的客户端

  首先，从 `.base_client.py` 导入 Fern 生成的基础客户端并扩展它们以创建您的自定义客户端。然后，添加您想要的任何方法。

  ```python title="src/<package>/client.py"
    from .base_client import BaseClient // 导入生成的客户端
    
    class YourClient(BaseClient): // 扩展生成的客户端
        def my_helper(self) -> None:
            print("Hello World!")
            def my_helper(self) -> None:
                print("Hello World")
  ```

  <Note>
    在 ElevenLabs 查看示例 [client.py](https://github.com/elevenlabs/elevenlabs-python/blob/main/src/elevenlabs/client.py)。
  </Note>

  ### 更新 `.fernignore`

  将 `client.py` 添加到 `.fernignore`。

  ```diff title=".fernignore"
  + src/<package>/client.py
  ```

  <Note>
    在 ElevenLabs 查看示例 [.fernignore](https://github.com/elevenlabs/elevenlabs-python/blob/main/.fernignore)。
  </Note>

  ### 使用方法

  Now your users can consume the helper function by importing it from the SDK.

  ```python
  client.my_helper()
  ```
</Steps>

## 添加自定义依赖项

<Warning title="专业版和企业版功能">
  此功能仅适用于[专业版和企业版计划](https://buildwithfern.com/pricing)。如需开始使用，请联系 [support@buildwithfern.com](mailto:support@buildwithfern.com)。
</Warning>

要添加您的自定义代码所需的包，请更新您的 `generators.yml`。

```yaml {4-7} title="generators.yml"
- name: fernapi/fern-python-sdk
  version: "..."
  config:
    extra_dependencies:
      numpy: '1.2.0'
    extra_dev_dependencies:
      requests_mock: '1.12.1'
```