首页 > 解决方案 > 如何在 django 项目中安装 swagger_client

问题描述

我在尝试运行以下代码时遇到了ModuleNotFoundError: No module named 'swagger_client'的问题。

如何导入 swagger_client?

我想访问自定义 api。

我的代码:

from __future__ import print_function
import time
import swagger_client
from swagger_client.rest import ApiException
from pprint import pprint


# create an instance of the API class
api_instance = swagger_client.BookingApi()
reservationId =  # Long | reservation id

try: 
    # Cancel reservation
    api_response = api_instance.cancel_reservation(reservationId)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling BookingApi->cancelReservation: %s\n" % e)

标签: python-3.xswaggerstrava

解决方案


我不确定它是否仍然相关,但是设置 swagger_ 客户端对我有用的是,在我下载了我想要使用的 API 的特定客户端之后,即,一旦我swagger 编辑器,我将其解压缩,然后在我将其解压缩到的同一文件夹中运行python setup.py install。然后我只选择了名为swagger_client的文件夹,并将swagger_client.egg-info复制到我的virtualenv/lib/python3.X/site-packages文件夹,我使用的所有其余包都位于该文件夹中。之后,我可以将 swagger_client 导入我的所有 Django 文件。


推荐阅读