You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swagger-Codegen生成Royal Mail API Python模块导入报错

Fixing ImportError with Swagger-Codegen 2.3.1 for Royal Mail REST API Python Module

I recently ran into a frustrating import error when using Swagger-Codegen 2.3.1 to generate a Python module for the Royal Mail REST API. The pip install . step worked perfectly, but when trying to import the module in Python, I got this error:

ImportError: No module named 'rm_tracking.models.inline_response200_error'

The issue? The correct module path should be rm_tracking.models.inline_response_200_error—notice the missing underscores around the 200 in the error message.

Reproduction Steps

  • Create a new directory and navigate into it
  • Place your Tracking_Swagger.yaml and config.json files in the directory
  • Create a swagger subdirectory
  • Run the generation command:
    swagger-codegen generate -i Tracking_Swagger.yaml -l python -c config.json -o swagger
    
  • Navigate into the swagger directory and run pip3 install .
  • Trigger the error by running import rm_tracked in Python

What's Causing This?

This is a naming consistency bug in the older Swagger-Codegen 2.3.1 version. When the tool generates model files from your Swagger spec, it converts names like inlineResponse200Error to snake_case (resulting in inline_response_200_error.py), but it fails to apply the same conversion in internal import statements—leaving references to inline_response200_error instead of the correctly named file.

Solutions to Fix the Error

1. Update the Swagger YAML's Model Names

Modify your Tracking_Swagger.yaml file to use snake_case for the problematic response model directly. For example, rename inlineResponse200Error to inline_response_200_error. When you regenerate the code, the filenames and import paths will match perfectly.

2. Configure Swagger-Codegen to Use Consistent Snake_Case

Add naming rules to your config.json to force the generator to apply snake_case consistently across files and imports:

{
  "modelNaming": "snake_case",
  "apiNaming": "snake_case"
}

Re-run the generation command with this updated config, and the tool will produce matching filenames and import references.

3. Manually Fix the Generated Code

If you don't want to adjust your Swagger config, you can patch the generated code directly:

  • Search through the rm_tracking module for any imports referencing inline_response200_error and replace them with inline_response_200_error
  • Double-check that the corresponding model file is named inline_response_200_error.py (rename it if necessary)

4. Upgrade Swagger-Codegen

Swagger-Codegen 2.3.1 is quite outdated—later versions (especially the 3.x series) have fixed many naming conversion bugs. Upgrading to the latest stable version of Swagger-Codegen and re-generating your module will likely resolve this issue without manual tweaks.

内容的提问来源于stack exchange,提问作者Alex Hellier

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:15:36